# Params\[:commit\] doesn't work

**URL:** <https://www.ruby-forum.com/t/params-commit-doesnt-work/165337>\
**Category:** Rails\
**Created:** [April 12, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337 "2009-04-12T15:55:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackyang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jackyang](https://www.ruby-forum.com/u/jackyang)\
**Post date:** [April 12, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337/1 "2009-04-12T15:55:43Z")

</div>

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" %\>&nbsp;&nbsp; \<%= 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

---

<div class="post-metadata">

**Author:** ![jackyang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jackyang](https://www.ruby-forum.com/u/jackyang)\
**Post date:** [April 12, 2009, 4:49pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337/2 "2009-04-12T16:49:40Z")

</div>

On Apr 12, 2:55 pm, Jack Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![jackyang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jackyang](https://www.ruby-forum.com/u/jackyang)\
**Post date:** [April 12, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337/3 "2009-04-12T17:01:43Z")

</div>

Frederick C. wrote:

> On Apr 12, 2:55ï¿½pm, Jack Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![jackyang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jackyang](https://www.ruby-forum.com/u/jackyang)\
**Post date:** [April 12, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337/4 "2009-04-12T17:10:08Z")

</div>

On Apr 12, 4:01 pm, Jack Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![jackyang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jackyang](https://www.ruby-forum.com/u/jackyang)\
**Post date:** [April 12, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/params-commit-doesnt-work/165337/5 "2009-04-12T17:12:23Z")

</div>

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.
