简体   繁体   中英

Why do I not have to pass an instance of Label to my route helper?

For route helpers like edit_admin_label_template_path(@label, @template) , it requires two instances to create the route, otherwise raising a RoutingError exception.

But for the route new_admin_label_template_path , it doesn't need an instance of Label. The current_page url on the page I am using this route for is http://localhost:3000/admin/labels/5/templates , so I am assuming it is pulling from the params[:label_id] , but I don't get how exactly. Also, why does it not do this on the other page where I have to pass both an instance of Label AND Template?

Regarding the route pulling in the label_id from the params, it does so from the URL. It matches against something that looks something like:

/admin/labels/:label_id/templates

where :label_id can be anything

When you are creating a new template, you only need to know the id of the label because the template id is not yet set. When you edit the template, the id for template is set, and you need to supply both that and the id of the label to the rails router. Note that this is dependent upon how you set up your nested routes. For more information on routing, you should check out the Rails guide on routing .

When you say "edit" it needs to now which template? and which label's template? this is the reason rails need both @label and @template.

But when you say "new" it doesn't need to now which template because template is the one that will be creating. just @label is fine.

btw you should take a look at Rails Routing and should listen:

Resources should never be nested more than 1 level deep.

Deeply-nested resources quickly become cumbersome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM