开发者

Problem after submitting a form

I'm new at ruby on rails and I built a form with a list with checkboxes inside. I asked here how to get the checked values with params and I got it to work. The idea now is to perform some action with the selected items if I click some button, and if I click another one then perform anoother action, but always staying at the same page after postback.

What's happening now, is that I'm getting to the actions, but to another url at the same time. For example:

I've got the following form in my view:

form_tag delete_profiles_path, :method => :put do 
   submit_tag 'Delete' 

   @my_announcements.each do |ann| 
       check_box_tag 'announcement[]', ann.id  
      end 
  end 

My Profiles controller:

  def delete
     #some action
  end

And routes.rb:

map.resources :profiles, :collection => {:delete => :put}

This will redirect me after submit to:

/profiles/complete, and my form is at /profiles/some id/my-announcements

.

Also, what开发者_如何转开发 if I add another button like the Delete one, how can I handle multiple actions when clicking them but staying at the same url?


First of all, why do you delete with PUT and not DELETE HTTP method? You should use default method for delete: use :method => :delete and in controller #destroy action (you can also use delete, but it's like new for displaying what you want to do, and destroy is like update).

Second - name of button used to submit form is send in params, so you can distinguish bu params which button was used to submit form.

Thirt - you cannot 'stay on same page' in Rails (or it will be possible, but against Rails). It should be like this:

  1. submit form to action in controller, most of time its url is same as '/profiles/' or '/profiles/id' and the action is distinguished bu method (POST, PUT or DELETE)

  2. distinguish what you want to do and try to complete action. If it is not possible (validation errors for example), then simply show same form with actual values and errors showing why it cannot be completed. If user refreshes page, and submits params again, he should get same page and same errors, and the action should not be completed too.

  3. if this controller completed request, make redirect to some other page. It works like this because if user refreshes page, then (if it was redirected) nothing happen, he gets back new page. If you would not redirect user would make second time same action, which is not always what you want. This new page can be page where the form was submitted from of course


Well,

I could partially solve this using ajax as follows:

remote_form_for :profile, :url => {:controller => 'announcements', :action => 'deactivate'}, :html => { :method => :put} do |f| 
  f.submit 'Publish', :confirm => 'Are you sure?'

This works great. What I'd like to do now is to add another "submit" or button, so I can perform several actions, so I guess the :url statement at the remote_form_for will be replaced for something like that per each button. For example:

Publish button: perform some action in some controller. Deactivate button: perform another.

Is it clear?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜