For SEO purposes, I've appended a slug to an id, like so:
/browse/12885-evergreen-instrumental
by using this in my model:
def to_param
"#{id}-#{slug}" || id
end
Ideally, I'd like to have the structure as so:
/browse/12885/evergreen-instrumental
I've tried several things via routes.rb to accomplish this, but to no avail yet.
Essentially, I'm trying to do this:
match "/browse/:id/:slug" => "tracks#show", as: :track
and than will want a redirect:
get '/browse/:track_id', to: redirect('/browse/%{track_id}/%{slug}')
Any ideas on how to get this to actually work?
Thanks in advance.
I think what you are looking for is the FriendlyId gem. It does exactly what you are trying to do.
Related
I am trying to show username instead of user_id i.e., (users/4).
i have tried with changing routes file, model file as:
class User < ActiveRecord::Base
def to_param
username
end
end
and routes file as:
get '/:username' => 'users#show'
on show action it must show as fb.com/username url.
thanks in advance.
You may use friendly_id gem for this. Here is Rails cast : Pretty URLs with friendly_id On how to use it. You can use the attribute/column you want in the URL.
With FriendlyId, it's easy to make your application use URLs like:
http://example.com/states/washington
instead of:
http://example.com/states/4323454
I have a site that currently has URLS that look like this
http://localhost:3000/public/category/1?category_id=1
That the SEO guy has requested be changed to this
http://localhost:3000/(:category_name)-leasing
where (:category_name) is the name of the category referenced by category_id=1 and is appended with "-leasing"
Is it possible to set this up without creating a new column in my category model for a permalink.
Currently I also have this in my routes.rb file:
get ':permalink', :to => 'public#show'
which displays items from the category mentioned above as
http://localhost:3000/item-name-from-permalink
Is this going to prevent a category using the same url structure? will rails be able to distinguish between a :permalink and a (:category_name)-leasing url or will the url need to something like:
http://localhost:3000/category/(:category_name)-leasing
Thanks in advance
class Category < ActiveRecord::Base
def to_param
"#{self.id}-#{self.title.parameterize}"
end
end
will produce url like http://localhost:3000/categories/1-some-good-title
I have a question about browser string in rails.
For example i have rails app with routes:
resources :posts
and this resource create :
post/:id
post/21
post/167
post/356
but i create a simple blog and i want to rename ':id' to
post/some-name
post/another-name
post/another-different-name
in post i have title, text field
but i dont know how do this
I know that this can be achieved through manipulation of the :id
can you post some link with detailed answer on this question, or some simple example
You can of course put anything you want in the URL and actually there is railcast about it:
http://railscasts.com/episodes/63-model-name-in-url
It is preferable (read: easier) to also keep model.id in the URL, or it means that post name MUSTÂ be unique, otherwise you can put anything you want:
/post/2465-my-pretty-post-name
Also, there is a gem friendly_id and related railcast:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Hope that helps.
Why do you want to change /post/:id ?
You can achieve something like /post/:id/comments
You can do that using nested resources like this in your routes.rb
resources :posts do
resources :comments
end
Check here for more details
http://guides.rubyonrails.org/getting_started.html
If you add the to_param method to the model then you can use that within your URL system.
class SomeModel < ...
def to_param
self.title
end
end
Then inside your controller, setup a filter to fetch the model using the title attribute instead of the ID attribute which is used for the find method.
before_filter :setup_record
def setup_record
#record ||= Record.find_by_title(params[:id])
end
You will have to ensure that your title stays unique and if you change it then you will either have to discard all other previous URLS or keep a history of older names.
I want to be able to have post permalinks appear in the root of the site. So, for example, a post with a permalink "hello-world" should appear as "mysite.com/hello-world", instead of "mysite.com/posts_controller/hello-world."
How would I go about doing something like this?
I believe that you already have a "slug" field in your posts model.
If your post controller has that into account, you just need to add the correct route for instance:
match '/:slug' => "Posts#show"
Otherwise, if don't have the slug in your model, you can use the Stringex plugin. It's an easy way to automatic create slugs for your posts.
class Post < ActiveRecord::Base
acts_as_url :title
end
It will create the slug from your title and save it to the slug column.
In the controller you can find the correct post like this:
def show
#post = Post.find_by_slug(params[:slug])
end
In your routes:
match '/:slug' => "Posts#show"
Then in your controller you could do something like:
Post.find_by_slug(params[:slug])
Note: you will need to generate this slug value and store it in the Post model.
Also have a look at friendly_id for a tried and tested way of doing this (if you need something more complex).
I currently have URLs which look like this:
things?category_id=6&country_id=17
and I would like to have URLs which look like this:
/printer_cartridges/united_kingdom
Is there a way in Rails 3, without hard coding all of the categories and countries in the router to have the URLs as I would like above, perhaps using find_by_name or the such like? What is the best way to approach this?
match '/:category_slug/:country_slug', :to => 'things#index'
Then you'll need to update your action to look up everything using params[:category_slug] and params[:country_slug] instead of the ids. Look at the slugged gem to generate slugs.
In your category model add the method
def to_param
"#{category_name.parameterize}/#{location_name.parameterize}"
end
where category_name and location_name are where you input where you have have the names stored.