Ruby on Rails page control - ruby-on-rails

I'm sorry, my English is bad. Question: I have model Pages with columns title, description etc. I can create, change, destroy these pages. I can see the contents of the link mydomain/pages/1. I need for each page has been template and route, so I can see the content on the link, for example maydomain/contacts. How to do it? Help me please.

One way to implement your own solution is to add this to your routes file:
get '/mydomain/:slug, to: 'pages#show'
This is a pretty general matcher, so add it to the bottom of your routes so it doesn't override others.
Then your controller show action will look something like:
def show
#page = Page.find_by_slug(params[:slug])
end
This of course assumes you have a slug column on your Pages table.

I'm assuming by "mydomain" you mean the root url of your site (e.g. myapp.example.com)
I'd suggest that you separate the problem into two parts:
Use an attribute other than id to identify an item in the url
Reduce the route so that the controller does not need to be specified.
For 1, have a look at this: Rails routes with :name instead of :id url parameters Note, that as #spickermann suggests friendly_id could be a good solution for you.
For 2, you will need to create a route without the controller name, and then specify the controller in the route definition. (See the Rails Routing Guide):
get ':param', to: :show, controller: 'pages'
For that to work, you will need to put it after (lower in routes.rb) so that it doesn't intefer with other routes. I'd also recommend adding a constraint to the route - to limit the wrong urls that could be routed to that rout.

Related

How does Airbnb route each article?

I am making a website with Ruby on Rails, referencing Airbnb, and I found it difficult to make the following URL structure.
https://www.airbnb.com/help/article/767/what-is-the-resolution-center
In this URL, it seems that there is an article resource under help, so 767 is the ID of the article. In addition, after the ID, there is the name of the page in the URL.
scope '/help' do
resources :articles
end
By doing this, I was able to route until this part.
/help/articles/'page_id'
But I have no idea what to do from here.
I generated article model(title:string, content:text), and I am guessing that each article(title and content) is displayed according to the id.
Here's my questions.
How does Airbnb route page_name after the article ID?
In the case that the title and content in the article table are displayed, how do you put hyperlinks or internationalize the contents with I18n?
Also, please tell me if my guess is wrong in the first place, and tell me how Airbnb routes each article.
Thank you.
To tell Rails to accept request under URL like /help/article/767/what-is-the-resolution-center is actually very easy when you do use get instead of nested resources:
get '/help/article/:id/:slug', to: 'help_atricles#show', as: 'help_article'
Given the above URL, Rails would extract the id and slug from the URL and route the request to the show method of a HelpArticlesController.
That controller method could be as simple as:
def show
#article = HelpArticle.find(params[:id])
end
And to build such URLs you can use the help_article_path helper method defined by the as: 'help_article' part:
<%= link_to(
#article.title,
help_article_path(
id: #article.id,
slug: #article.title.parameterize
)
) %>
Read more about this routing method in the Rails Guides
Btw. I didn't use the slug part of the URL because it feels to me like it makes the URL look nicer and might be SEO relevant, but it feels useless from the application's point of view. You might want to use the slug part to identify the locale in which the article should be shown if you do not want to use the browser's locale setting.

Rails routing issue, can't figure out the link path

Let me fair from the outset, and tell you that I've 'solved' the problem I'm describing. But a solution that you don't understand is not really a solution, now is it?
I have a resource, Newsbites. I have an index page for Newsbites. All my CRUD actions work fine.
I created a separate index (frontindex.html.erb) that acts as the front page of my website to show the newest Newsbites. The formatting is different from my normal index so readers get a larger photo, more of the text of the article(more ads too:).
In my routing table, I have the following statements:
resources :newsbites
get 'newsbites/frontindex'
root 'newsbites#frontindex'
Rake routes show the following:
newsbites_frontindex GET /newsbites/frontindex(.:format) newsbites#frontindex
If I load my website from the root (localhost:3000), it works great. There is a separate menu page that is rendered at the top, and it loads fine. I can click on all links, except the 'Home' link, and they work fine.
The 'Home' link is:
<%= link_to 'Home', newsbites_frontindex_path %>
When I click on the linked, I get the following error:
Couldn't find Newsbite with 'id'=frontindex
The error points to the 'show' action of my Newbites controller. Here are the frontindex and show def from the controller. They appear exactly as I'm posting them:
def frontindex
#newsbites = Newsbite.all
end
def show
#newsbite = Newsbite.find(params[:id])
end
I don't get it. Why is the show action being called by newbites_frontindex_path when there is both a def and views that match? Now, I can get around this by simply pointing home to root_path. But that doesn't help me understand. What if this was not the root of the site?
Any help would be greatly appreciated.
Actually I'm very surprised your code worked at all. A route must define two things
Some sort of regex against which the URL of the user is matched (newsbites/frontindex is different than newsbites/backindex)
What do you want to do for a given URL ? You want to point to a controller action
Rails won't usually "guess" what that action is. Or maybe, he was still able to "guess" that you wanted to use the newsbites controller, but it didn't guess the action right this time :(.
You should declare the root like this, which is what you did
root 'controller#action'
For the rest, there are two ways you can declare it. I prefer the second one
resources :newsbites
get 'newsbites/frontindex', to: 'newsbites#frontindex'
resources :newsbites do
# stuff added here will have the context of the `newsbites` controller already
get 'frontindex', on: :collection # the name of the action is inferred to be `frontindex`
end
The on: :collection, means that 'frontindex' is an action that concerns ALL the newsbites, so the URL generated will be newsbites/frontindex.
On the other hand get 'custom_action', on: :member, means that the custom_action targets a specific item, the URL generated would look like newsbites/:id/custom_action
EDIT : Rails also generate path_helpers based on the route declaration
get 'test', to: 'newsbites#frontindex'
get 'test/something', to: 'newsbites#frontindex'
resources :newsbites do
get 'frontindex', on: :collection
get 'custom_action', on: :member
Will generate path helpers
test_path
test_something_path
# CRUD helpers : new/edit/show/delete, etc. helpers
frontindex_newsbites_path
custom_actions_newsbite_path(ID) # without s !
You can always override this by adding an as: option
get 'custom_action', on: :member, as: 'something_cool'
# => something_cool_newsbites_path
Rails routes thinks that frontindex is an id. That's what the error message says. So it goes to GET newsbite/:id which maps to show.
You need to find a way let Rails routes know that frontindex is not an id.
On a side note: The order in which you define routes matters. The first one matched will be used. If you have GET newsbite/:id and GET newsbite/frontindex then the one that appears first will be matched. In your case this is the first one.
Maybe try to change the order.

Rails rename model name in url dynamically

In Rails, I have a model named item. Each item belongs_to category (another model), which has an attribute called name.
When seeking any item, I see the url as /item/:id
instead, I would like the url to show up as /'item.category.name'/:id
e.g. if the item belongs to category whose name is "footwear", then, I would like to see the url as /footwear/:id. And if there is another item that belongs to category whose name is "clothing", then I would like to see the URL as /clothing/:id
Is this possible?
If you setup a route like
# config/routes.rb
RouteTest::Application.routes.draw do
get ':category_name(/:id)', to: 'items#show', constraints: {id: /\d+/}
end
It will map to the show action in your ItemsController, and in there, you will have access to params[:category_name] and params[:id]. With this information, you should be able to get the data you want and render it.
Note that this route however will likely have the undesirable effect of masking any routes that follow. You could use rails advanced route constraints to further narrow down 'which values would be considered valid category_names' but this wouldn't be a very scalable or manageable approach.
For example, you could do something like
RouteTest::Application.routes.draw do
get ':brand_name(/:id)', to: 'items#show', constraints: lambda { |request| BrandList.include?(request.params[:brand_name]) }
# etc. ...
get ':category_name(/:id)', to: 'items#show'
end
but this only really works well when the BrandList is a finite list that you could setup during application initialization.
A better, more scalable approach might be to design your URLs like
/brand/adidas
/brand/teva
/shoes/1
/shoes/2
/jackets/45
IOW, prefix known namespaces like brand with an appropriate human friendly URL prefix and use category based route as a catch-all at the bottom.
Hope this helps.

How can I make my rails app display url's as the user name?

In my Rails app a user can create one registry and the url's is for example:
http://localhost:3000/registries/3
How can I make that url be for example:
http://localhost:3000/erinwalker
Thanks in advance.
The simplest way of doing this is by creating a new route at the bottom of your routes.rb file:
match "/:username" => "registries#show"
It's important to put this at the bottom of your routes.rb file as this will match any route of the format "/whatever" that doesn't match the routes preceding it.
This will point to the show action of your registries controller. So within that action you can just do
#user = User.find_by_username params[:username]
#registry = #user.registry
The friendly_id gem and other slugging gems is, IMO, the easiest approach.
(And here's a friendly_id railscast.)
That said, it's easy to create a route that accepts legal slug values and redirect or look it up yourself.

how do I make the URL's in Ruby on Rails SEO friendly knowing a #vendor.name?

My application is in RoR
I have an action/view called showsummary where the ID has been passed into the URL, and the controller has used that to instantiate #vendor where #vendor.name is the name of a company.
I would like the URL to be, rather than showsummary/1/ to have /vendor-name in the URL instead.
How do I do that?
All of these solutions use find_by_name, which would definitely require having an index on that column and require they are unique. A better solution that we have used, sacrificing a small amount of beauty, is to use prefix the vendor name with its ID. This means that you dont have to have an index on your name column and/or require uniqueness.
vendor.rb
def to_param
normalized_name = name.gsub(' ', '-').gsub(/[^a-zA-Z0-9\_\-\.]/, '')
"#{self.id}-#{normalized_name}"
end
So this would give you URLs like
/1-Acme
/19-Safeway
etc
Then in your show action you can still use
Vendor.find(params[:id])
as that method will implicitly call .to_i on its argument, and calling to_i on such a string will always return the numerical prefix and drop the remaining text- its all fluff at that point.
The above assumes you are using the default route of /:controller/:action/:id, which would make your URLs look like
/vendors/show/1-Acme
But if you want them to just look
/1-Acme
Then have a route like
map.show_vendor '/:id', :controller => 'vendors', :action => 'show'
This would imply that that it would pretty much swallow alot of URLs that you probably wouldnt want it too. Take warning.
I thought I'd mention String#parameterize, as a supplement to the tagged answer.
def to_param
"#{id}-#{name.parameterize}"
end
It'll filter out hyphenated characters, replace spaces with dashes etc.
Ryan Bates has a great screencast on this very subject.
Basically you overload the to_param method in the Vendor model.
def to_param
permalink
end
Then when you look up the resource in your controller you do something like this:
#vender = Vender.find_by_name(params[:id])
But the problem with this is that you'll have to make sure that the vendors' names are unique. If they can't be then do the other solution that Ryan suggests where he prepends the the id to the name and then parses the resulting uri to find the item id.
You do this by modifying the routes that are used to access those URL's and changing them to use :name, rather than :id. This will probably mean that you have to write the routes yourself rather than relying on resources.
For instance add this to the routes.rb file:
map.with_options :controller => "vendor" do |vendor|
vendor.connect "/vendor/:name", :action => "show"
# more routes here for update, delete, new, etc as required
end
The other change that will be required is that now you'll have to find the vendor object in the database by the name not the id, so:
#vendor = Vendor.find_by_name(params[:name])
Internally (at least to my knowledge/experimentation) whatever parameter name is not specified in the URL part of the route (i.e. not within the "/Controller/Action/:id" part of it) is tacked on to the end as a parameter.
Friendly ID
http://github.com/norman/friendly_id/blob/26b373414eba639a773e61ac595bb9c1424f6c0b/README.rdoc
I'd have to experiment a bit to get it right, but there's two primary parts to the solution.
1) Add a route.
in config/routes, add a line that sends requests of the form baseurl/controller/:vendor-name to the action showsummary, (or maybe a new action, show_summary_by_vendor_name)
[also, if you planned on using baseurl/:vendorname, that's fine too]
For convenience, make sure the parameter is something like :vendor-name, not the default :id
2) Write the controller action.
In the controller file, either edit your showsummary action to differentiate based on whether it's called with an id or with a vendorname, or just write a show_summary_by_vendor_name. (depending on best practices, and what route you wrote in 1. I don't know off the top of my head which is preferable)
You can then do
#vendor = Vendors.find_by_name(params[:vendor_name])
or something like that, and then render it the way you would in regular showsummary.
3) Use that as the link.
Once you confirm that baseurl[/controller?]/vendor-name works, and shows the summary, make sure all the links in your application, and elsewhere, use that link. Off the top of my head, I can't remember how difficult it is to integrate a custom route into link_to, but I think it's doable. Most search engines [google] rely heavily on links, so good SEO will have you using those named links, not the numbered ones. I think. I don't know much about SEO.
Take a look also at this quck start to SEO for Rails

Resources