How to implement a "delete all" button in Rails? - ruby-on-rails

I have an index page of elements, and I'm trying to implement a "Delete" button that will send a delete request to the controller with a list of the elements user has checked. So far I've done the following
#routes.rb
resources :messages, :except=>[:update,:edit] do
member do
delete :delete_all
end
end
#index.html.haml
=button_to "Delete", {:controller=>"messages", :action => "delete_all"}, :method=>"delete"
...
=check_box "message", "mark"
#messages_controller.rb
def delete_all
....
end
I've been trying to do it RESTfully, but I've come across routing errors and other tricky problems. Like, for instance, when I used pure AJAX I come across the problem with the before_filter that wants to authenticate the user, and it doesn't let my request through.
Can anyone explain to me what I need to do? How do I implement this button?

Try using collection do for the route, since this is how you use it in your view (without an :id).

Related

Rails 4: Simple search form with wrong redirect

I have some trouble with simple search form. When I pass information in the form on page:
/search
and press submit button it collects data and searches but instead of reloading current page it's passing params to index:
/claims?utf8=✓&claim_id=49&phone_number=%2B34(44)444444
But I want to show the result of search on current page (without any AJAX).
If I manually pass those params and change link it's working greate!
/search?utf8=✓&claim_id=49&phone_number=%2B34(44)444444
My routes.rb
Rails.application.routes.draw do
resources :administrators
resources :claims
root :to => "sessions#landing"
...
get 'search' => 'claims#search'
post 'claims/new' => 'claims#new'
post 'claims' => 'claims#create'
end
My search function in controller:
def search
#claim = Claim.search(params[:claim_id], params[:phone_number])
end
How can I modify my routes to do that?
So, couple of things are happening here. You need to to do 2 things
Make sure your controller action can handle multiple types of requests
Make sure you have 2 routes, 1 for posting (since you aren't using ajax) and 1 for getting
controller action
if request.post?o
#claim = Claim.search(params[:claim_id], params[:phone_number])
else
Claim.all (or whatever you want to display when the page is fresh before a search has been done)
end
And your routes are a little messy so lets fix that when we add the post.
resources :claims do
collection do
get 'search'
post 'search'
end
end

Remove nested attribute rails

I have a Post model that
has_many photos
The Post model has
accepts_nested_attributes_for :photos
In my Post edit form i show all the images that are currently associated with the post
<% for photo in #post.photos %>
<%= image_tag(photo.avatar.url(:thumb)) %> <%= link_to "Remove Photo", post_photo_path(#post, photo), :method => :delete %>
<% end %>
When hovering over over the Remove Photo link the path looks like so
http://localhost:3000/posts/17/photos/45
I get uninitialized constant PhotosController, but that's expected as there isn't one.
My question is do i create one and have a destroy action in there that will just delete that photo.. Is this the way to go about it or is there a better way?
EDIT
Photos Controller
def destroy
#photo = Photo.find(params[:id])
#photo.destroy
redirect_to edit_post_path, :notice => "Successfully deleted Photo"
end
Routes
resources :posts do
resources :photos
resources :departments
end
Rake Routes
edit_post GET /posts/:id/edit(.:format)
Thanks
The option you described, invoking the destroy action of a PhotosController, would certainly do the trick and would probably be the simpler and quicker solution. Be careful that if a user makes changes to other aspects of the Post and then clicks a link to delete the Photo in the same form, their changes would probably get lost. However, using AJAX might help alleviate this concern.
Another option would be to leverage rails existing support for nested forms. Because you are already using a nested form with accepts_nested_attributes_for you're about halfway there. If you haven't seen this yet, check out Ryan Bates' podcast on Nested Forms:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
And if you're willing to pay (trust me, it's worth it), the revised solution is a little nicer:
http://railscasts.com/episodes/196-nested-model-form-revised
The meat and potatoes of what you're interested in is towards the middle to the end of the podcast where Ryan uses the hidden _destroy field in the nested element's params to indicate to the controller than when the parent object is updated, the nested association should be destroyed.
You can also find an off the shelf gem that wraps up this same Rails/Javascript functionality that Ryan implemented from scratch. I've used cocoon in the past and have been quite pleased.
This technique is a little more complicated but I tend to prefer it in this case because the focus of the form is to modify a Post, not a Photo. In the first solution, you would have to make the PhotosController#destroy action redirect back to the form. Or if you use AJAX, then you would have to write javascript that was tailored to the layout of your Post HTML to properly hide/remove the element after it was destroyed. This may be ok for small applications, but as the project grows you may find yourself trying to use the same PhotosController for other work-flows and that could become brittle to maintain.
My opinion is that the best thing to do would be to create a PhotosController to handle the destroy action. That way you will preserve the Single Responsibility Principle, and have less workaround with adding custom routes.
Also, there is the option to put that one action in the PostsController if you are really sure you won't be adding any additional actions connected to Photos (like tagging photos, or whatever).
It's really up to you, but i believe the first one is easier.
If your going with the second option, be sure to create a route for the destroy photo action in the PostsController and the link should be pointing at that exact action.
Your routes.rb:
resources :posts do
resources :photos, :only => [:destroy]
resources :departments
end

How to call a custom method in a Rails controller from a View

I have set up a site that is correctly using basic CRUD functionality succesfully. However, when I try to add a custom method to my controller I cannot seem to hook it up to a link_to call. I keep getting a method not found error.
The Controller method looks like this:
def complete
return render :text => "Complete"
end
and my call in the View looks like this:
<%= link_to 'Complete', complete_list_task_path(#list,#task) %>
This same call works for my Edit method, so I'm not sure what I'm doing wrong. Do I need to do anything special when the method is not a basic CRUD call?
The only relevant part of my route file looks like this (List and Task are nested resources. List has many tasks, and task belongs to a list):
resources :lists do
resources :tasks
end
I have also tried adding post "complete" => "lists/:id/tasks/:id#complete", :as => "complete" to my route to see if it would help to implicitly try to call it, but I still got a "method not found error".
Any help in figuring out how to make this call would be greatly appreciated. Thank you!
See Adding More RESTful Actions in the Rails Routing guide for details; the nutshell is that if you want routing to recognize anything other than the standard methods, you need to add it.
You need to declare the method in the router, 'resources' refers to the 7 crud actions (index, new, create, edit, update, delete, show).
Off the top of my head, I think you'd need:
resources :lists do
resources :tasks do
member do
post :complete
end
end
end
The nesting makes me less confident, but that's the general thing you need to do.

Rails routing of a controller's functions query

So I've got a Users controller, and it has (amongst others) a function called details.
The idea is that a user can go to
localhost:3000/user/:user_id/details
and be able to view the details of :user_id.
For example, I have a user called "tester".
When I go to the uri: http://localhost:3000/users/tester/details
I'd want the details function to be called up, to render the details view, and to display the information for the user tester.
But instead I get an error saying that
No action responded to tester. Actions: change_password, create, current_user, details, forgot_password, index, login_required, new, redirect_to_stored, show, and update_attributes
And I understand that to basically mean that if I wanted to access details, I should really be using
http://localhost:3000/users/details
Except that that isn't really working either... >.<
That is instead bringing me to http://localhost:3000/users/details/registries
(which is the default path that I'd stipulated for anybody trying to view users/:user_id, so again, that's working the way I wanted it to)
Point is: Can anybody help and tell me how I can go about getting
users/:user_id/details to work the way I want it to and display the details of :user_id?
Thanks!
Are you using resources? If your routes look like:
map.resources :users
You could make it:
map.resources :users, :member => { :details => :get }
That would allow GET requests for the URL /users/:id/details
More info here: http://guides.rubyonrails.com/routing.html#customizing-resources
I think that your problem is with setting routes in such way, that instead of :user_id you have :login (or whatever) in url /users/tester instead of /users/34. Probably you should take a look at to_param (1st example, 2nd example, 3rd example).
If you want to have another option in routes (besides default REST routes), you can add :member => {:details => :get} if you are using map.resources (#dylanfm answer) or just map it like in #Salil answer.
In order to get routes like "users/:user_id/details" change following in routes.rb
map.users 'users/:user_id/details', :controller => 'users', :action=>'details'

Rails - RESTful Routing - Add a POST for Member i.e(tips/6)

I'm trying to create some nice RESTful structure for my app in rails but now I'm stuck on a conception that unfortunately I'm not sure if its correct, but if someone could help me on this it would be very well appreciated.
If noticed that for RESTful routes we have (the uncommented ones)
collection
:index => 'GET'
:create => 'POST'
#:? => 'PUT'
#:? => 'DELETE'
member
:show => 'GET'
#:? => 'POST'
:update => 'PUT'
:destroy => 'DELETE'
in this case I'm only talking about base level action or the ones that occur directly inside i.e http://domain.com/screename/tips or http://domain.com/screename/tips/16
but at the same time I notice that there's no POST possibility for the members, anybody knows why?
What if I'm trying to create a self contained item that clones itself with another onwer?
I'm almost sure that this would be nicely generated by a POST method inside the member action, but unfortunately it looks like that there's no default methods on the map.resources on rails for this.
I tried something using :member, or :new but it doesn't work like this
map.resources :tips, :path_prefix => ':user', :member => {:add => :post}
so this would be accessed inside http://domain.com/screename/tips/16/add and not http://domain.com/screename/tips/16.
So how would it be possible to create a "default" POST method for the member in a RESTful route?
I was thinking that maybe this isn't in there because it's not part of REST declaration, but as a quick search over it I found:
POST
for collections :
Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.
for members :
Treats the addressed member as a collection in its own right and creates a new subordinate of it.
So this concept still the same if you think about the DELETE method or PUT for the collection. What if I want to delete all the collection instead just one member? or even replace them(PUT)?
So how could I create this specific methods that seems to be missing on map.resources?
That's it, I hope its easy to understand.
Cheers
The reason they aren't included by is that they're dangerous unless until secured. Member POST not so much as collection PUT/DELETE. The missing member POST is more a case of being made redundant by the default collection POST action.
If you still really want to add these extra default actions, the only way you're going to be able to do it, is it to rewrite bits of ActionController::Resources.
However this is not hard to do. Really you only need to rewrite two methods. Even then you don't have to rewrite them fully. The methods bits that you'll need to add to those methods don't really on complex processing of the arguments to achieve your goal. So you can get by with a simple pair of alias_method_chain.
Assuming I haven't made any errors, including the following will create your extra default routes as described below. But do so at your own risk.
module ActionController
module Resources
def map_member_actions_with_extra_restfulness(map, resource)
map_member_actions_without_extra_restfulness(map, resource)
route_path = "#{resource.shallow_name_prefix}#{resource.singular}"
map_resource_routes(map, resource, :clone, resource.member_path, route_path, :post, :force_id => true)
end
alias_method_chain :map_member_actions, :extra_restfulness
def map_default_collection_actions_with_extra_restfullness(map, resource)
map_default_collection_actions_without_extra_restfullness(map,resource
index_route_name = "#{resource.name_prefix}#{resource.plural}"
if resource.uncountable?
index_route_name << "_index"
end
map_resource_routes(map, resource, :rip, resource.path, index_route_name, :put)
map_resource_routes(map, resource, :genocide, resource.path, index_route_name, :delete)
end
alias_method_chain :map_default_collection_actions, :extra_restfulness
end
end
You'll have to mess around with generators to ensure that script/generate resource x will create meaningful methods for these new actions.
Now that we've covered the practical part, lets talk about the theory. Part of the problem is coming up with words to describe the missing actions:
The member action described for POST in the question, although technically correct does not hold up when applied to ActionController and the underlying ActiveRecord. At best it is ambiguous, at, worst it's not possible. It makes sense for resources with a recursive nature (like trees,) or resources that have many of a different kind of resource. however this second case is ambiguous and already covered by Rails. Instead I chose clone for the collection POST. It made the most sense for default post on an existing record. Here are the rest of the default actions I decided on:
collection
:index => 'GET'
:create => 'POST'
:rip => 'PUT'
:genocide => 'DELETE'
member
:show => 'GET'
:clone => 'POST'
:update => 'PUT'
:destroy => 'DELETE'
I chose genocide for collection DELETE because it just sounded right. I chose rip for the collection PUT because that was the term a company I used to work for would describe the act of a customer replacing all of one vendor's gear with another's.
I'm not quite following, but to answer your last question there, you can add collection routes for update_multiple or destroy_multiple if you want to update or delete multiple records, rather than a single record one at a time.
I answered that question earlier today actually, you can find that here.
The reason that there's no POST to a particular member is because that member record already exists in the database, so the only thing you can do to it is GET (look at), PUT (update), or DELETE (destroy). POST is designed only for creating new records.
If you were trying to duplicate an existing member, you would want to GET the original member in a "duplicate" member action and POST to the resource root with its contents.
Please let me know if I'm missing what you're asking.

Resources