Rails linking to a nested resource from outside - ruby-on-rails

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

Related

Rails: complicated routing

Say I have a standard Rails application with five models: Topic, Post, Author, Comment, and CommentAuthor. I want Posts available like domain.com/:author_name/32 and I want Topics available like domain.com/12.
It's like I want one model available under root without disrupting the others' natural hierarchy. Is that even possible in Rails?
UPDATE
It's because I keep getting errors like this when loading resources:
{"controller"=>"topic", "action"=>"show", "post"=>"assets", "id"=>"social-icons", "format"=>"css"}
For the Topics, assuming that your controller is Topics Controller
get '/:id', to: 'topics#show'
For the Posts, it would be
get '/:author_name/:id', to: 'posts#show'
Also, please have a read through the guides: http://guides.rubyonrails.org/routing.html
You will probably want a nested route, with
resources :authors do
resources :posts
end
Assuming you did your model associations properly that would be the start of your routing. to get domain.com/author_name/32, you'll need to customise it a little. Perhaps
https://github.com/norman/friendly_id
can help you out.

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.

Best practice for further actions in rails controllers

i'm just writing my first app in rails and i wonder, if there is a best practice to do the following:
i have a customer model created by a scaffold and pumping it up. a customer has to be displayed in a google map, so if go to /customers/23, the customer information are displayed. additionally i have a link within this page to show the user in a map (with a query ui dialog that comes up via ajax).
The question for me is, how does this fits in the normal crud structure of the model. Should i do like creating an action, called "show_map" and give it an extra route additionally to the resources routes? How do you handle this kind of things?
Lets do it like
resources :customers do
resource :map, :only => [:index]
end
it will generate routes like this
{:action=>"show", :controller=>"maps"} customer_map GET /customers/:customer_id/map(.:format)

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