Rails: complicated routing - ruby-on-rails

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.

Related

Rails nested resource creation on separate pages

resources :books do
resources :chapters
end
Let's assume I have the above properly nested resources. I want to create a page where I create parent book resources and another page to create the chapters resources. When creating chapters, I want users to be able to select parent books they created.
Right now I have...
protected
def find_book
#book = Book.find(params[:book_id])
end
...in the chapter controller but I believe this only works when there is already a book id present in the URL. So to create a new chapter I would have to visit "rootpath/book/book_id/chapter/new" when I want to be able to create chapters on a separate page.
Although I'm really not sure how to approach the problem, right now my plan is to put an association(?) form on the chapter creation page that links the nested resources.
The problem is, I'm really new to web development and I'm not sure if I'm approaching this right at all. How would I put a form that sends :book_id to the chapter controller? Would this method work at all? Are there more efficient ways to go at it?
I realize my questions might be a little vague but Any help would be greatly appreciated!
The dull answer is: your proposal does not make sense with only the nested route.
The nested route implies that upon accessing the chapters#new action, you already know exactly which book that should contain the chapter.
But on the bright side: you can use both nested and non-nested routes at the same time.
If you want to keep the nested route, but also provide a new and create actions that lets the user choose the desired Book for the chapter, you can add a non-nested route for Chapter creation.
For example:
resources :books do
resources :chapters
end
resources :chapters
Note that your controllers may need to be rewritten a bit to accomodate the dual routes.
If you want, you could create both resources in the same page. Look up accepts_nested_attributes_for to do that. It's really easy, once you get the hang of it.

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

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)

whats the difference between map.resources and map.resource

Hi Could any one let me know the difference.
Thanks in Advance-- Vam.
resource will create a singluar resource i.e. all the routes of resources but without index.
It's useful for things like "account".
Here's an explanation from the official Ruby on Rails guides:
Resource Routing: the Rails Default
Singular Resources
One is plural, one is singular.
Good example on
http://guides.rubyonrails.org/routing.html#singular-resources
Basically if your representing something which has more than one possible URL - i.e. you can look up multiple different books or authors, use map.resources. If you want to just look up a singular resource. For example the current user or the one and only Admin page, use map.resource

Can someone please explain to me in clear, layman's terms what the deal is with mapped resources and named routes in Ruby on Rails?

I've been using Ruby for the first time on a project at my work, so I am still somewhat learning the ropes (and loving every minute of it).
While I understand the point of the map.connect functions in the route.rb file, I don't understand the "resources" and "named route" features of Rails. I have my Rails book here and read it over several times, but I still don't get it. The named routes I kinda get - I think that they are either rules, either explicitly defined, or calculated by a code block, but the resources are a complete mystery to me; the only thing I've gleamed rom them is that you just NEED them if you want some of the cool stuff to work, such as being able to call 'resource_path' (and its awesome related family of methods).
My current project has:
map.resources :application_forms
map.resources :sections
map.resources :questions
map.resources :seed_answers
map.resources :question_types
map.resources :form_questions
map.resources :rules
map.resources :form_rules
..but my Rails book has this awesome kinda "has_many" and "only" type hashes and parameters hanging off them and I can't work out exactly when I am supposed to use them, nor what the benefit is.
Can anyone set me straight?
Named routes are just that; a route with a name attached, so that you can easily refer to it when you want to generate a URL. Among other things, it can eliminate ambiguity.
A resource is basically a 'thing' that you want to have routes to manipulate. When you define that 'sections' is a resource, what you're doing is saying "I want a route to get all the sections. I want a route to add a new section. I want a route to edit an existing section. I want a route to delete a section." That sort of thing. These routes point to standardized method names like index, new, edit, and so on. Each of these routes will have a name assigned based on what it is; so there is now a route named 'edit_section'.
The :has_many parameter lets you say that a certain kind of thing has sub-things. For example, you can say map.resources :sections, :has_many => [:questions]. This means that a question belongs to a section, and this will be reflected in the url and the route. You'd get urls like '/sections/27/questions/12' and named routes like 'section_questions'.
The :only parameter says "only make routes for these actions"; you could use it if you only want to allow listing, viewing, and adding items, not editing or deleting.
Honestly the Rails Routing Guide will give you a good explanation in about as plain wording as you can get. Just know that a resource route == RESTful route and you're good to go.
We all struggled with understanding resources and REST when DHH introduced it to the Rails community at the first RailsConf in 2006, so it is not wonder you have trouble grasping the concept.
I admit there is much better and more up-to-date explanations of the concepts today, but back then, right after David's keynote, I wrote a blog post in which I, from discussion with other conference attendees, tried to understand and explain it. It might help you, as it doesn't take for granted that you know everything about REST as more recent articles do.

Resources