Trouble with params not being submitted with multiple form submit buttons

Viewed 43

I'm trying to have two different submit buttons for a rails form, but I somehow don't get the params when submitting either one. What I have is

<%= form_with(model: @post, local: true, multipart: true) do |f| %>
.
.
.
<%= f.button 'Save', name: :commit, value: "Save" %>
<%= f.button 'Save and publish', name: :commit, value: "Save and publish" %>

then I have a method

def save_and_publish?
  params[:commit] == "Save and publish" 
end

which I'm calling as follows

def update
  if @post.update(post_params)
    flash[:notice] = "Saved."
  else
    redirect_to request.referrer, flash: {error: @post.errors.full_messages} and return
  end
  @post.update(active: true) if save_and_publish?
end

but I'm getting an error "param is missing or the value is empty: post" when trying to submit either one of the buttons since I have

def post_params
  params.require(:post).permit(....
end

I don't understand why isn't the post being submitted with the params since I'm excplicitly stating in the form that model: @post?

I can make this work by adding

<%= f.hidden_field :post, value: @post %>

but this isn't how it's supposed to work.

0 Answers
Related