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" %> <%= 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 12, 2:55 pm, Jack Yang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Apr 12, 2:55�pm, Jack Yang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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. > > FredThanks 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 12, 4:01 pm, Jack Yang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---