I am trying to show flash message after hitting the update endpoint. I want to keep user on the same page and tell him that the info has been updated. I have turbolinks enabled.
Here is how my action looks like:
def update
@form = Form.find(params[:id])
if @form.update(form_params)
respond_to do |format|
flash.now[:notice] = "Form updated successfully"
format.html { render 'edit' }
format.js { render 'edit' }
# format.html { redirect_to edit_form_path(@form), notice: "Form updated successfully" }
format.json { render json: {message: "Form updated successfully", form: @form} }
end
else
respond_to do |format|
format.html { render 'new'}
format.json { render json: {errors: @form.errors}, status: :unprocessable_entity }
end
end
end
I have tried both flash
and flash.now
both yields no result. If you see the line which is commented out that will redirect to the same page and render the flash.
here is how I am printing the falsh:
<%= notice %>
<%= flash.notice %>
I have used both but they don't seem to work.
From my observation, I can see that Turbolinks hit the format.js
endpoint. Can anyone help me with this?
Edit
This is how my form looks like:
<%= form_with(model: @form, method: 'put') do |f| %>
<!-- name -->
<div class="field">
<label class="label">
From Name
<sup class="is-danger">*</sup>
</label>
<p class="control">
<%= f.text_field :name, class: "input" %>
</p>
<% if @form.errors.any? %>
<p class="help is-danger">
Form name <%= @form.errors[:name].first %>
</p>
<% end %>
</div>
<!-- create button -->
<div class="control">
<%= f.submit class: "button is-primary" %>
</div>
<% end %>