Rails: How to avoid drawn out urls with nested models - ruby-on-rails

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.

Related

What is the "_path" method in ruby on rails?

I'm learning RoR, and I'm getting very confused by the "_path" method as it's used in Controllers and Routes. To be more specific, I'm referring to the many different calls that take the syntax "(something)_path". So far as I know, they all seem to either encode or manipulate a URL or link. I'm having a hard time mastering the use of this type of method because I can't figure out what it's core functionality is supposed to be.
For example, I could use the following code to redirect an old URL structure to a page of listed Tweet instances in my config/routes.rb file:
get '/all' => 'tweets#index', as: 'all_tweets'
Only now can I use the following in an .erb file. Notice the "_path" code at the end of the line.
<%= link_to "All Tweets", all_tweets_path %>
I could also use the following code to create a link to an edit page (and another action) in a different .erb file:
<p><%= link_to tweet.user.name, edit_tweet_path(#tweet) %></p>
I've tried reading through my study materials as well as the RoR documentation, but I always end up more lost than when I began. Does anybody know the low-level definition of this "_path" method?
Helper
It's called a route helper, which means that Rails will generate them to help provide you with resource-based routing structures. I'll explain more in a second
--
To explain properly - Rails is just a framework.
Like all software, it is a series of files loaded in a particular order. As such, Rails creates a series of helper methods in the booting process. These "helper" methods can then be used throughout your application to call functionality / information as you require:
The Rails framework provides a large number of helpers for working
with assets, dates, forms, numbers and model objects, to name a few.
These helpers are available to all templates by default.
In addition to using the standard template helpers provided, creating
custom helpers to extract complicated logic or reusable functionality
is strongly encouraged. By default, each controller will include all
helpers. These helpers are only accessible on the controller through
.helpers
The route helpers (which are generated from your config/routes.rb file give you the ability to call routes which are resourceful. These might seem strange to begin with, but once you understand them, will help you inexorably.
--
Resourceful
To give you more clarity - Rails routes are known as resourceful
This means they are constructed around resources. To give you a brief definition of this, you need to appreciate that the resources of your application are the pools of data you can add to, and pull
from.
To explain further, because Rails is object orientated. If you're new, this won't mean very much, but keep it in mind, as when you progress through the language / work, you'll begin to see why this is important.
Object orientated programming puts OBJECTS at the center of the flow. Typically, you'd put logic at the center, but with OOP, it's the objects. This is very important for us, as it means that everything you do in Rails is based around the objects you can create.
As per the MVC principle (which, again, is what Rails is built on), you'll create / invoke your objects from your Models:
This means that if you want to create a series of routes to "CRUD" (Create Read Update Destroy) your objects, Rails is able to create the routes necessary to do that. This is where the resources directives come from inside the routes file:
Hope this helps!
Actually, these paths are generated based on your routes.rb. If you run this command at your project, you would be able to see all available on your app
rake routes
For example, if I declare my resources in routes.rb like this
resources :posts
then I would automatically have following available paths
posts_path
post_path
new_post_path
edit_post_path
If you use some strange abc_path which has not been declared in routes.rb, then you will get errors.
Hope this is helpful, you will definitely need to work more with Rails and then eventually you will understand all of these things :)
you could find definition for these methods in rails repository:
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L127

nested or normal routing in 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.

Ruby on Rails 2.3.8: Is there a way to get the controller name from a model?

Like, lets say I have the model called: Vehicle.
Is there any command such as Vehicle.controller that would return :vehicles_controller or "vehicles_controller" ?
Not that I'm aware of, but its easy to create such a helper:
def controllerize(model)
"#{model.name.tableize}_controller"
end
Better question is why do you want it? Sounds like a bit of a code smell—there aren't a lot of good reasons for your model to know anything about your controllers.
Why? It's routes that determines paths not models.
Making your model aware of routes makes no sense at all. Models are there to deal with business logic and data. Routes are the domain of controllers.
If you have a route defined as resource :vehicles in your routes.rb then you have routes available to you in the form of vehicle_path, vehicles_path etc... to find out what routes you have just run rake routes from your command line in your apps root folder.
It's just a naming convention and good practice that gets you some things for free, for doing it the Rails way, that a controller for User model is often called UsersController. If you keep with a convention, then you can write a method that does that for you. Otherwise, the answer is: "No, sorry".

Root-level routing with Ruby on Rails?

How do I efficiently enable root-level routing with Ruby on Rails?
For example, instead of having:
/questions/a-question-here
I want:
/a-question-here
What technology would I use, and how would I configure the routing to enable this?
EDIT: I also have other models such as videos and users, so I'd like the routing to match other controllers as well. For example, how do I get /username to go to some action in the Users controller and /some-video-title to go to some action in the Videos controller?
Thanks for all your help, people.
Rails has a very configurable routing layer built in. Good documentation here:
http://guides.rubyonrails.org/routing.html
Your route could end up looking something like this:
match ":id" => "questions#show"
Depending on where you put that, it could override a lot of other routes, so be aware of that.

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

Resources