Forum: Ruby on Rails params[:commit] doesn't work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
24429774a7859c9283caf73d3b1cb8aa?d=identicon&s=25 Chaoran Yang (jackyang)
on 2009-04-12 15:55
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 %>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit "Save" %>&nbsp;&nbsp;
    <%= f.submit "Publish" %>
  </p>
<% 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-12 16:49
(Received via mailing list)
On Apr 12, 2:55 pm, Jack Yang <rails-mailing-l...@andreas-s.net>
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
24429774a7859c9283caf73d3b1cb8aa?d=identicon&s=25 Chaoran Yang (jackyang)
on 2009-04-12 17:01
Frederick Cheung wrote:
> On Apr 12, 2:55�pm, Jack Yang <rails-mailing-l...@andreas-s.net>
> 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-12 17:10
(Received via mailing list)
On Apr 12, 4:01 pm, Jack Yang <rails-mailing-l...@andreas-s.net>
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
24429774a7859c9283caf73d3b1cb8aa?d=identicon&s=25 Chaoran Yang (jackyang)
on 2009-04-12 17:12
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.
This topic is locked and can not be replied to.