Creating a child-class object on rails - ruby-on-rails

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.

Related

Rails Restful convention for records found by user account

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.

Rails Active Admin with nested resources

I have two models (resources and tool_tip_infos) and I used active admin with nested resources and to achieve this I have defined like this:
ActiveAdmin.register ToolTipInfo do
belongs_to :resource
end
but if I am calling http://abc.com/admin/resources/17/tool_tip_infos/117/edit or http://abc.com/admin/resources/17/tool_tip_infos/new then it is giving following error:
undefined method `resource_id' for #<Resource:0xb1073e4>
Apart from these two actions(new and edit), others are working as expected.
Actual problem is naming convention like we have used resources that is already used in active admin so there is some code conflicts due to the resources that's why it was not working properly.
when I was change the other name like resources to available_models then it is working fine.
Thanks.

Rails linking to a nested resource from outside

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])

Rails incorrectly generates route for a resource with :controller set

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.

Nested resource route helper not working

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

Resources