Is it possible to append text to a route's URL, so instead of, say,
http://site/page/2
it comes out
http://site/page/2-cool-stuff-here
?
This should help you out: http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Since you appear to be okay with keeping the object's id in the url, you can override to_param like so:
class Article < ActiveRecord::Base
def to_param
"#{id} #{name}".parameterize
end
end
which would return .../articles/1-[article name] (As pointed out by the FriendlyId RailsCast mentioned by #Benjamin Tan).
If you desire more functionality than just that, you should check out the FriendlyId gem (see the RailsCast link above).
Related
I feel this is should be easy but I'm stuck. What's the best way to map a route and have it point to specific url?
User types/click http://acne.com/category-name and it will get http://acne.com/?category[]=1
I don't want to redirect to ?category[]=1 though. I want to make it SEO friendly.
Use the friendly_id gem.
Here is an example o how it could work:
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name
end
#category = Category.friendly.find "category-name"
#category.to_param
redirect_to #category # the URL will be /category/category-name
If I understood your question correctly, you want to show the category when user types in acne.com/category-name.
Add this in your config/routes.rb:
get '/:category-name', to: 'category#show'
This will route the request to your categoryController's show method with the user-typed category name in the "category-name" parameter. You can handle what is displayed there (use params[:category-name] to find out the category name the user has typed in)
Sorry for the lack of code tags, I'm on mobile.
for now when i click user with id = 1, on url bar its
users/1
I want to change it to
users/[encrypt]
or
users/some_user
is there any way to do that on rails?
What about using a permalink instead of the users id? i.e. users/[permalink] and you can configure the permalink to anything you like as long as it is a unique value
Checkout the friendly_id gem: https://github.com/norman/friendly_id
Rails uses to_param method when displaying object in url.
If you change to_param method in user it will, be used to display data instead of id.
By default rails has implemented to_param to return id of the object.
For example
class User < ActiveRecord::Base
...
def to_param
"#{self.first_name}-#{self.last_name}" # or whatever you want to use
end
...
end
In your url you will have /users/first_name-last_name or whatever your to_param method returns. By default to_param returns id and in url you get /users/4. and in your controller you can find user with id 4, but when you change to_param method, you have to change respectively the way you fetch user from database.
Example:
I change my to_param method to return nick_name from database, and it is unique for the particular user, that I can use to find user from database.
In router
change the mappings for params
get 'users/:nick_name', 'users#show'
In controller
User.find_by :nick_name => params[:nick_name]
like others in this post says , i use
i use
https://github.com/norman/friendly_id this way:
# app/models/secret.rb; this would go in the model you want to obfuscate
class Secret < ActiveRecord::Base
has_friendly_id :code, :use_slug => true
validates :name, :uniqueness => true
def code
Digest::SHA1.hexdigest self.name
end
end
it’s simple. If your security needs are serious you’d probably want something a little more complex (not to mention more layered than a basic obfuscation technique), but I wanted to share an out-of-the-box way to use a gem that already exists (and may even be in use in your app already)
Ruby on Rails 4.0. I have a 'Specialty' model that 'admins' can modify using the standard resource routes. However there is also a separate consumer facing controller that is used to display those 'Specialties' in a pretty fashion. As such I have the following routes:
get "specialty/:name" => "site#specialty", as: :site_specialty
resources :specialties
The site#specialty controller action is as follows:
def specialty
#specialty = Specialty.find_by_name(params[:name])
end
This results in urls like the following percent escaped routes:
/specialty/project%20management
I would rather have something like this:
/specialty/project_management
How do I replace the spaces with underscores and still look up the correct model in the controller action? Any side notes on security also appreciated
Try using to_param:
Model:
class Specialty < ActiveRecord::Base
def to_param
name.parameterize
end
end
Controller:
def specialty
#specialty = Specialty.find(params[:id])
end
That should do it...
References:
http://guides.rubyonrails.org/active_support_core_extensions.html#to-param
https://gist.github.com/cdmwebs/1209732
http://railscasts.com/episodes/63-model-name-in-url
The answer by #manishie is good, but there is also a gem that handles this for you (and much more), called Friendly ID. It is based on the same to_param trick as previously mentioned, but also has options to handle other special characters and handle collisions.
class Specialty < ActiveRecord::Base
extend FriendlyId
friendly_id :name
end
Specialty.friendly.find(params[:name])
My task is to be able to see the path of the current category or product in browsing bar.
At this moment I just can see current category like this
localhost:3000/categories/smalcinataji
but I want like this
localhost:3000/categories/atkritumu-parstrades-tehnika/smalcinataji
To create pretty urls I am using gem called FriendlyId from this example http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast
Thanks!
FriendlyId can take a method to construct the slug.
class Person < ActiveRecord::Base
friendly_id :category_and_subcategory
def category_and_subcategory
"#{my_category_method}/#{my_subcategory_method}"
end
end
Note that there might be an issue with routing due to the additional slash, but there's certainly a fix for this, too, if nescessary.
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.