Params[:commit] doesn't work

I have a form with two submit buttons: “Save” and “Preview”. In my
controller for this form, I do different things based on
params[:commit]. But somehow the params[:commit] always evals “Save”,
even when I click Preview button. Why? any idea will be appreciated.

This is my view:
<% remote_form_for(@post) do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :id %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :body %>
<%= f.text_area :body %>

<%= f.submit "Save" %>   <%= f.submit "Publish" %>

<% end %>

This is my controller:
def create
@post = Post.new(params[:post])
@post.id = params[:post][:id]
begin
if params[:commit] == ‘Publish’
@post.published = true
@post.save
flash[:notice] = ‘Post was successfully published.’
redirect_to posts_path
else
@post.published = false
@post.save
flash[:notice] = ‘Post was successfully saved.’
end
rescue => ex
flash[:notice] = ex.message
render :action => :new
end
end

On Apr 12, 2:55 pm, Jack Y. [email protected]
wrote:

I have a form with two submit buttons: “Save” and “Preview”. In my
controller for this form, I do different things based on
params[:commit]. But somehow the params[:commit] always evals “Save”,
even when I click Preview button. Why? any idea will be appreciated.

Because you’ve got an ajax form. The javascript that serializes the
form for you doesn’t know which submit was clicked (unlike the browser
code that would run with a normal form), and so it will have the
commit parameter in twice (once with each value) and rails will
discard one of them. You could probably use submit_to_remote with an
appropriate
:with option to do this.

Fred

Frederick C. wrote:

On Apr 12, 2:55�pm, Jack Y. [email protected]
wrote:

I have a form with two submit buttons: “Save” and “Preview”. In my
controller for this form, I do different things based on
params[:commit]. But somehow the params[:commit] always evals “Save”,
even when I click Preview button. Why? any idea will be appreciated.

Because you’ve got an ajax form. The javascript that serializes the
form for you doesn’t know which submit was clicked (unlike the browser
code that would run with a normal form), and so it will have the
commit parameter in twice (once with each value) and rails will
discard one of them. You could probably use submit_to_remote with an
appropriate
:with option to do this.

Fred

Thanks Fred. I haven’t try using submit_to_remote, but I think you are
right.
I just changed the form back to normal form without ajax. This time the
“Publish” button works correctly but the “Save” button behaves weird.
The first time I click it, it saves the post. But after the post is
saved, if I click save again it will “Publish” the post. Why?

This is my controller:

def create
if params[:post][:id] != “”
@post = Post.find(params[:post][:id])
@post.update_attributes(params[:post])
else
@post = Post.new(params[:post])
end
if params[:commit] == ‘Publish’
@post.published = true
if @post.save
flash[:notice] = ‘Post was successfully published.’
redirect_to posts_path
else
render :action => :new
end
else
@post.published = false
if @post.save
flash[:notice] = ‘Post was successfully saved.’
render :action => :new
else
render :action => :new
end
end
end

On Apr 12, 4:01 pm, Jack Y. [email protected]
wrote:

Thanks Fred. I haven’t try using submit_to_remote, but I think you are
right.
I just changed the form back to normal form without ajax. This time the
“Publish” button works correctly but the “Save” button behaves weird.
The first time I click it, it saves the post. But after the post is
saved, if I click save again it will “Publish” the post. Why?

Have you looked in your logs to see what is being submitted ? (ie is
the browser doing something weird or is it your code ?)

Fred

Oh, I found the problem myself.
I have a hidden field hold the post_id after I saved the post. So when I
click save button with a form has post_id field it will trigger the
update action, not the create action, which will publish the post.