Rails Active Admin with nested resources - ruby-on-rails

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.

Related

Rails: Configure routes to attempt to find records from two classes

This is my current route configuration:
resources :organizations, path: ''
resources :users, path: ''
I want to create a similar experience to what GitHub does. When using GitHub, you can access organization and user profile pages by entering "https://github.com/#{username}"
Now, the routes configuration above leads to the obvious problem that accessing organizations works fine while accessing a user fails because Rails only considers the organizations route and does not attempt to find a user.
Note: I am using friendly id to use usernames in my URL's and also made sure that usernames are unique across both ActiveRecord classes.
How do I do what I want to do?
You can create additional controller like PageOwnerController and pass request to it:
get ':page_owner_nick', to: 'page_owners#show', as: :page_owner
In show action you can manually find desired record by params[:page_owner_nick].
Advice: it looks like you have many a lot of similar logic between users and organizations - take a look on STI. Using STI allow you to write common code easier, but at the same time to separate different logic.

Creating a child-class object 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.

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

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

RoR - add content in relation to other model

so i have got two models with each one controller:
Model Project has_many Themes
Model Theme belongs_to Project
in my routes file i added resources :projects and also for themes. Now i can add a project with localhost/projects/new which works fine and i can add Themes with localhost/themes/new. But thats not the way i want.
I only want to add Themes related to a project. Whats the best way to do this? I tried something like this: match "projects/:project_id/themes/new" => 'themes#new', :as => 'themes' which seems to work, but after submitting my new form nothing happens. the new form gets rendered again without a error message or something like that. my form tag in html gets rendered as the following:
<form accept-charset="UTF-8" action="/projects/3/themes/new" class="new_theme" id="new_theme" method="post">
do you have any ideas what went wrong? is there a best practice for something like that, because i think its a often wanted model.
You want to use nested resources
resources :projects do
resources :themes
end
This situation is called nested resources.
In your routes define:
resources :projects do
resources :themes
end
It will create exactly the url you described, as well as a bunch of helper methods. See the rails guides for a complete list, but here's an example:
To reach a single theme you would use projects_theme_path(#project, #theme), or to view all the themes for a project you would use projects_themes_path(#project). Again, see the rails guides for the full explanation and all the helpers.
Also, at any time you can run rake routes to see the EXACT helper methods as they are set up for your project.
You should also check out https://github.com/josevalim/inherited_resources which makes modeling and implementing these routes stupid simple.

Resources