Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass multiple arguments to nested route paths in Rails?

I am new to Rails and normally set up a link_to helper for a normal unnested route like so:

link_to "Delete", article_path(article.id), method: :delete, data: {confirm: "Are you sure?"}

However I am learning about nested routes and it seems I need to provide the path with two arguments for example:

link_to "(Delete)", article_comments_path(comment.article_id, comment.id), method: :delete, data:{comfirm: 'Are you sure?'}

However this does not seem to work. I have seen that you can format the link_to like this:

link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }

But this seems confusing to me as there is no path defined and the values necessary for the path arn't directly specified either.

Is it possible to format a nested link_to path like the unnested one above or does it need to be formatted as shown in the third example? If so could someone try to explain how this array format is translated into a url for Rails to execute?

Thanks

Route:

article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - comments#destroy

like image 529
Cu1ture Avatar asked Aug 12 '14 16:08

Cu1ture


People also ask

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

Can rails create paths and URLs from an array of parameters?

In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:

What is resource routing in rails 2?

2 Resource Routing: the Rails Default. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

What is each method in resource route in rails?

Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. When your Rails application receives an incoming request for: it asks the router to map it to a controller action. If the first matching route is:

What is a nested route?

However, this is a post about nested routes, not just rendering normal routes. Typically with nested routes, the parent Route acts as a wrapper over the child Route. This means that both the parent and the child Route s get rendered.


1 Answers

I think your route would be something like articles/:article_id/comments/:id so you can do:

<%= link_to "Delete", article_comments_path(article_id: comment.article_id, id: comment.id), method: :delete, data:{comfirm: 'Are you sure?'} %>

And your 3rd link should be

<%= link_to 'Destroy Comment', polymorphic_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>

For details check Polymorphic routes

Update

You just need to pass locals to your partial like:

<%= render "comment", locals: {article: comment.article, comment: comment} %>

and then use

<%= link_to "Delete", article_comments_path(article.id,comment.id), method: :delete, data:{comfirm: 'Are you sure?'}%>
like image 97
Mandeep Avatar answered Oct 18 '22 11:10

Mandeep