I have a nested route like so:
resources :apps do
resources :issues
end
the helper for seeing all issues related to an app is as follows:
app_issues_url(app)
but now I want to use a helper to point to a specific issue of a specific app like apps/1/issues/1 but i don't know how to use that helper. what is the helper for this url?
you can pass both instances to url helper like
app_issue_url(#app,#issue)
you can also use
app_issue_path(#app,#issue)
or
url_for([#app,#issue])
see rails doc for more info
Related
Say I have a cart that I find based on a current user record. How would a restful URI look for that.
You could say:
PUT /carts/56/cart_items/67 For an Update example
However changing this URL would not effect the program as the controller most likely has something like
#cart = current_user.cart
Ideally the URL would look more like:
PUT /user_cart/cart_items/67
Is there a rails router convention to build a system like this short of a bunch of custom routes?
I ended up with
namespace :checkout do
resources :cart_items
resources :carts
end
Even though cart_itmes are an embedded resource of carts their scope isn't part of the URI so the route is under a checkout namespace which makes since... kind of. Lol programming philosophy.
I added a controller named "Triage" to my application, and added a PUT route as follows:
resources :triage do
collection do
put :process_multiple
end
end
Instead of the expected process_multiple_triage_path route, it seems it is processed as process_multiple_triage_index_path:
process_multiple_triage_index_path PUT /triage/process_multiple(.:format) triage#process_multiple
triage_index_path GET /triage(.:format) triage#index
POST /triage(.:format) triage#create
Answer: Turns out it is because triage is seen as a singular resource by Rails, thus by way of convention, you'd request the "index" of the resource.
This was because of the singular form of triage. Rails noticed that triage was used, instead of triages, and thus as a result of convention, the request would be of the index. I've linked below another StackOverflow that explains this well.
You can use resource :triage instead of resources (which also won't create an index route automatically either)
More info here
It's on the collecion, so Rails appends an _index. If you want to overwrite it, just change to:
resources :triage do
collection do
put :process_multiple, as: :proccess_multiple_triage
end
end
And now you'll be able to use process_multiple_triage_path
In my rails application I have a Program that has many Enterprises.
In my program/show, I want to have the option to simply create a new Enterprise that belongs to the program shown.
I tried using nested resources and a routing helper like new_program_enterprise_path, but it gave me the No route matches "{:action=>"new", :controller=>"enterprises"}" error.
This is what my routes.rb has:
resources :programs do
resources :enterprises
end
The problem is how you are using the routing helper. The new_program_enterprise_path should take an instance of Program in its parameters like this new_program_enterprise_path(#program) this would result to a path that looks like this
/programs/:program_id/enterprises/new.
I have a (simplified) routing like:
resources :users do
resources :messages
end
resources :searches
I have a Search model/resource, following Railscasts http://railscasts.com/episodes/111-advanced-search-form
Now when my search is complete, I'd like to have a link to the actual message which is under user_message_path, but I don't have access to it under the Search resource since it's not nested within.
Is this a problem with the way I've routed/designed it or is there a Rails way of accessing this, like a helper method?
I think you're trying to relate two things which are not related. You have users which have nested messages, and you have searches. Just link to the message with user_message_path, it doesn't have to be under the Search resource or related to the search resource at all.
The link probably looks like
user_message_path([#user, #message])
So here's the routes in question:
resources :subjects, path: 'library' do
resources :modules, controller: 'subject_modules'
end
When I write form_for [#subject, #subject_module] (with those two set to what you'd expect), Rails tries to generate "subject_subject_module_path".
When I remove the :path for the subjects resource, the generated helper remains the same (as expected).
What it should be is "subject_module_path"; I suspect the problem is that Rails looks at the controller for the modules resources and uses that instead of its actual path, i.e. it builds this helper:
subject_ + subject_module_ + path
From what I've gathered so far, it's pointing towards the possibility of a bug, but is it possible it's more something on my side or something intended by design?
For now, I suppose this is usable as a temporary fix:
form_for([#subject, #subject_module], url: subject_module_path(#subject, #subject_module))
I'm using Rails 3.1.3.
If this is a form (update or create) then your routes should be pointing to
subjects_module_path
which should generate the URL
/library/modules/
have you tried pointing your form at
form_for [:subjects, #subject_module]?
which will then end up at your subject_modules controller
To be honest, your naming is very confusing and probably not helping. Ideally subject_modules should just be modules.
This is something that occurs due to the design of Rails.
The following is what the Rails API docs say in relation to url_for:
Relying on named routes
Passing a record (like an Active Record or Active Resource) instead of
a Hash as the options parameter will trigger the named route for that
record. The lookup will happen on the name of the class. So passing a
Workshop object will attempt to use the workshop_path route. If you
have a nested route, such as admin_workshop_path you’ll have to call
that explicitly (it’s impossible for url_for to guess that route).
But saying explicitly declaring a helper is required when you have a "nested route" is in accurate. If the nesting occurs under a resource (without certain kinds of routing options, more on this later), Rails will be able to generate the path without any issues.
form_for appears to at some point, like url_for, call polymorphic_url in order to generate the URL to target, which in turn calls build_named_route_call.
You can see that build_named_route_call simply generates underscored_versions of the ModelNames passed in, and joins them together with underscores.
Bringing that back to my routing:
resources :subjects, path: 'library' do
resources :modules, controller: 'subject_modules'
end
Since the subjects resource is under /library/ by setting :path, its helpers remain as subjects_*, and thus Rails has no problems generating a URL for it when passed a Subject. The subject_modules resource (named as such since Rails reserves the name Module for models) however, has had its named helpers changed from its model name through the setting of :controller.
Mystery solved.