nested or normal routing in ruby on rails - ruby-on-rails

I am writing blog in rails and I am in half way. I got stuck somehow with routing while implementing edit and deleting comments for an article. I have 3 specific doubts in my mind.
1). To get this kind of url (http://localhost:3000/articles/54/comments/56/edit) for blog, Do I need to follow nested routes. I mean
resources :articles do
resources :comments
end
2) can I get this kind of url without nested routing? If yes, Please give some explanation.
3) Currently, I am trying to implement nested routes ( may be changed after your response). For this kind of routing, Do I need to create "comments" folder inside "articles" folder of views? or can I create "comments" folder directly under views folder to get nested routes working.
Thanks for your time.

To get this kind of url for blog, Do I need to follow nested routes.
No, you can use the method match to create any route yourself.
can I get this kind of url without nested routing? If yes, Please give some explanation.
With method match,
match 'articles/:article_id/comments/:id/edit' => 'comments#edit'
more info in the rails guides
Currently, I am trying to implement nested routes ( may be changed after your response). For this kind of routing, Do I need to create "comments" folder inside "articles" folder of views? or can I create "comments" folder directly under views folder to get nested routes working.
For nested routes you should not change the standard folder structure from Rails - that is, the comments folder should be under views folder, not under comments folder. The only moment I'm aware of when you should nest folders in your views/controllers/models directory is when you use a namespace.
Edit:
You should look in the semantic of your application to decide if you need nested resources or not. If the CRUD actions you do in your comments are relative to the posts, then you should keep the nested resources, otherwise not.
In this case, I see clearly a nested resource.

Related

Self nested infinite routes

Is there a way to self nest a resource in the routes infinetelly?
Suppose the following scenario: I want to create several pages that may be self nested one to another, like a products page has many product pages and each product page has several sub-pages etc.
The resource would be a page in a tree structure , like with the awesome nested set gem.
In case an admin can create pages at unknown depth level how would I make the routes?
The above example has to produce a url like /:friendly_id_of_level_1/:friendly_id_of_level_2/.../:friendly_id_of_level_n
I've tried going with dynamic routes but it has many drawbacks this way.
Any suggestions?
I do a similar thing to this in one of my apps with this route:
map.connect "/c/*modules", :controller => "content", :action => "show"
(Note this is using rails2 routing syntax, you might need to update it).
This will resolve this url
/c/123-foo/456-bar/789-baz/653-qux
to the content#show action, with params set like
params = {"modules"=>[123-foo", "456-bar", "789-baz", "653-qux"]}
The modules are in a tree structure, so i can use the sequence of module ids in params[:modules] to make a breadcrumb chain and any other heirarchical data, and i use the last one in the array as the "current" one to actually show to the user.
Note: I put the "/c/" at the start of the url to separate these nested routes out from all my other routes: otherwise it's very greedy and will match pretty much any url on your site. That's not a problem if you want to always have it as your "catchall" route down at the bottom of your routes file, but if not then you'll need to add something to make it distinct. Obviously this doesn't need to be "/c/", you could have anything which will stop it colliding with your other routes.

Rails: How to avoid drawn out urls with nested models

I've noticed that doubly nested urls have nasty urls. Is there a way to "prettify" these without changing the model relationships? Or is it cleaner to change model relationships. Say comments-->answers-->questions and change how they interact?
I recommend you take a read through the Rails Routing guide. It will give you all of the information you need to create beautiful custom URLs: http://guides.rubyonrails.org/routing.html
You can define your own routes in the routes.rb file and have them point to whatever you want.

Opinions on RESTful route for DELETEing of nested resources?

Given a typical nested resource of a photo with comments, the route to create a comment would look something like:
POST /photos/{photo_id}/comments
Now, for deleting the comment, would you still use a "nested" route? ex:
DELETE /photos/{photo_id}/comments/{comment_id}
or
DELETE /comments/{comment_id}
The pro of the nested route is that it mirrors the creation URL and doesn't require any additional entries in routes.rb. The pro of using a top-level URL is that you technically don't need the photo_id to get the comment to delete.
Thoughts?
How you model the comments resource depends heavily how you see the resource comments.
In case a comment could exist without a photo and could be associated with 0 to N resources like photo then you should model your comments like this
GET /comments/{comment_id}
DELETE /comments/{comment_id}
PUT /comments/
POST /comments/{comment_id}/associations/photo/{photo_id}
In case a comment is always associated with a resource and cannot exist without being associated with a resource then you should stick with
POST /photos/{photo_id}/comments
DELETE /photos/{photo_id}/comments/{comment_id}
I guess the confusion how to model the comment is driven by the database model where every comment gets a unique id that is unique among all comments and not just unique in a photo_id and comment_id combined key. I suggest not to let the database model leak to the resource model and go for a model that fits your conceptional understanding of the resource.
Can you GET this?
GET /comments/{comment_id}
I guess not. But if you can't GET a resource, you can't DELETE it, too.
So only your second option is RESTful.
Personally, I'm just using nested route for delete, means i used:
DELETE /photos/{photo_id}/comments/{comment_id}
to delete comments of a photo. If i use:
DELETE /comments/{comment_id}
so i have to create one more route for this? I don't find any reasons to create a separate route for delete, I think it's not necessary. The nested resources have create url and path for us, and they are follow convention, why don't we use them? I just want to keep it simple and will not do extra work for things already have.

What is a "resource" in Rails?

Dumb question but I have some lingering confusion of what, exactly, a "resource" is in Rails. The term is used everywhere but I get a funny feeling it might be being used rather loosely. It's referenced in the model, the controller and, quite literally, in routes.rb.
Is it the specific route? For example, map.resources maps the 7 RESTful "resources". So an example of one resource would be the call to, say, the index action of a particular class's controller?!?
Is it a reference to the whole page/object being retrieved? or perhaps, more narrowly, a database table? or the row being retreived?
Is it something else?
Anyway, hopefully someone can set me straight...
Any object that you want users to be able to access via URI and perform CRUD (or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generally a database table which is represented by a model, and acted on through a controller.
For example, you might have a User resource (with a users table in your DB). This is represented by a User model, is mapped to users_controller with map.resources :users (which then generates routes like /users (a collection of User resources) and /users/1 (a specific User resource).
You act upon those resources by using the appropriate HTTP method when making calls to those resources. POST to the resource collection (/users) creates a new record; GET retrieves a list of resources (/users) or a specific user (/users/1). PUT updates a specific user (/users/1/), and DELETE destroys that user. The URLs are the same, but the result (and controller action) may be different based on the HTTP verb. The idea, though is that /users/1 always means "I'm interacting with the User that has ID #1", regardless of the action.
Here's a good article discussing how most developers think that "Resource" is synonomous with the database table, the argument, I guess, being that mapping to the resource is mapping the controller to that database table (or, with ActiveResource, to another REST url).
Basically, I think a "resource" is "persisted data." map.resources maps the 7 RESTful actions to a particular suite of persisted data.
But I haven't thought about it too much in depth. Good question!
I think they probably mean it in the general web sense, i.e., Resource (Web):
the referent of any Uniform Resource Identifier
I don't think it has anything to do with database tables.
open your model folder, that is a hint of what resources you have!
example: users, pictures, comments...
A lot of people here say that resources refer to the database tables you have. It might be true sometimes but not necessarily true always. I could give you a lot of examples where you don't have a corresponding table in your database for a particular resource. Hence asssociating it with tables is rather wrong.
I would define a resource as a route which maps to related requests. So instead of declaring separate routes for the actions you want to do you can simply declare them using a resourceful route.In Rails, a resourceful route provides a mapping between HTTP requests and URLs to controller actions.
So say you define resources :users in config/routes.rb. You can now use a number of helpers to the controllers in your application like edit_user_path which returns users/edit .
Here's a good link: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Routing/Mapper/Resources.html
Which basically says: Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code:
resources :photos

custom routing in rails

I have a rails application that has a model named graphic and as any rails application there is normal routing like this
something.com/graphics/1
something.com/graphics/2
something.com/graphics/3
which will take you to the appropriate show pages. That I understand this is done in my routes by this statement
resources :graphics
Now come to find out the client wants to have the url to be like this
something.com/1
something.com/2
something.com/3
so if there is a number directly after the root url then like it to the graphic show action....any ideas on how to do this without messing up any other models
Seems like you are looking for:
match "/:id" => "graphics#show"
You can refer to http://guides.rubyonrails.org/routing.html for any further modifications.

Resources