Frederick Cheung wrote:
> On Apr 12, 2:55�pm, Jack Yang <[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
-- 
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to