views:

32

answers:

2

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?

A: 

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

MBO
Ok Well. What I'd like to do is to perform a couple of actions when the user selects items from the list by checking the check boxes. Once they click the button they want(delete was jut an example) I want the website to stay at the same page, it's not crazy what I'm asking for. let's imagine you're using Gmail and you want to select five items and then click Archive, you're not going to be redirected to another url, you'll be at the same one than before. I jut want to do that. Thanks.
Brian Roisentul
@Brian You're sure that Gmail stays on same page? I checked 'basic HTML' (just to use basic HTML, not fancy JavaScript) version and it does redirect after submit, so to me it's just like I shown in answer. It doesn't use RESTfull urls for this, but that's another story. If you want to do it without page reload, then You need Ajax, but it's little more complicated.
MBO
Well, I use the "fancy" version and when I select multiple emails and then I click "Mark as Read" it will do it with ajax. So, again, my question is how can I have multiple ajax submits within a form?
Brian Roisentul
@Brian Ok, that's "easy" - just bind some JavaScript function (depends what JavaScript library you use, jQuery is popular, Prototype is included by default in Rails) to your buttons (may be two different functions), make Ajax request to your controller, do what you want to do in controller, respond with json (`respond_to` method in controller) or anything else (can be rendered new form), and when you get your respond (preferably asynchronously) display result to user and change form to reflect changes.
MBO
A: 

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?

Brian Roisentul