whats the difference between map.resources and map.resource - ruby-on-rails

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

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.

Rails 3 routing : customize a resourceful route

I've got some Documents (and a DocumentsController), which are sorted using limited, fixed set of categories. I'd want my routes to take into account these categories, so my urls would look like :
/documents/:category/:id
/documents/:category/new
/documents/:category/:id/edit
...and so on, which should allow me to access params[:category] in order to filter the results. Is there a simple way to achieve this, that would still generate path helpers ? Or im i wrong to do this that way ?
You can provide a path to a resource (as you mentioned):
# config/routes.rb
resources :documents, :path => 'documents/:category'
This would give you the following routes:
/documents/:category
/documents/:category/new
/documents/:category/:id/edit
/documents/:category/:id
I am not sure in this case what purpose the category capturing will serve, since you can reference the document by its primary key. This key most likely does not repeat across categories.
It's not hard to customize paths in Rails 3.
match '/documents/:id', to: 'documents#show', as: :document would give you the path helper document_path(:id). This will work even for an ID that's a string rather than a number, so extending this pattern to /documents/:category/:id/edit should be no problem.

REST Rails 2 nested routes without resource names?

I'm using Rails 2.
I have resources nested like this:
- university_categories
- universities
- studies
- professors
- comments
I wish to use RESTful routes, but I don't want all that clutter in my URL. For example instead of:
/universities/:university_id/studies/:study_id/professors/:professor_id
I want:
/professors/:university_id/:study_id/:professor_id
(I don't map professors seperately so there shouldn't be a confusion between this and /professors/:professor_id since that route shouldn't exist).
Again, I want to use RESTful resources/routes...
Also note, I am using slugs instead of IDs. Slugs for studies are NOT unique, while other are. Also, there are no many-to-many relationships (so if I know the slug of a professor, which is unique, I also know which study and university and category it belongs to, however I still wish this information to be in the URI if possible for SEO, and also it is necessary when adding a new professor). I do however want to use shallow nesting for "administrator" URIs like edit, destroy (note the problem here with Study since it's slug is not unique, though)...
I would also like some tips on how to use the url helpers so that I don't have too much to fix if I change the routes in the future...
Thank you.
It doesn't seem like map.resources will provide you with this functionality, but you could use something like (untested)
map.show_professor "/professors/:university_id/:study_id/:professor_id", :controller => "professors", :action => "show"
and then similar routes for the other actions.
There might be a better solution, but this is the only way I can find that would work, since it seems map.resources assumes it is in the form of /resources/(:resource_id)
You can use REST this way, you just have to do all the actions yourself instead of using the shortcut.
As an example of an edit, you can just use
map.edit_professor "/professors/:id/edit", :controller => "professors", :action => "edit"

Rails 2.3.5 Creating RESTful routes for a model named "Software"

Topic says it all guys, my program needs to be able to keep track of the "software" that belongs to a user, as such I have a MVC for "Software". However, I can't use RESTful routes at the moment since my plural and singular names are the same, it's "software" for both. Can anybody help me either, create new named routes or suggest a new name for my MVC that still implies "software" but has different singular/plural?
Why don't you call it SoftwareItem so you can have SoftwareItems...?
If you really need to have your model named Software, you should do what is needed for uncountable-noun-named models:
map.resources :software, :singular => "software_item"
That way you'll have helpers like
new_software_item_url
software_item_url(software)
etc. Otherwise it's hard for Rails to tell whether you're using "software" as singular or plural :)

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