I am using nested routes in my application (rails 3.2) as follows:
resources :networks do
resources :groups
end
The route for groups show page is as follows
network_group GET /networks/:network_id/groups/:id(.:format)
How can I change the parameters to :network_name and :group_name respectively? Also, I would like to rename the route to group_path (instead of network_group_path ). I would like these changes reflected for all the routes without having to use 'match' for the individual routes.
Is it possible to have something like group_path(#network, #group) return '/networks/global/groups/all', where 'global' and 'all' are both 'name' attribute for the respective models. (by default I get the id's in the url)
We can do that by adding a to_param method in our model
http://railscasts.com/episodes/63-model-name-in-url
In your network and group model, add a #to_param method that returns what you want in the URL (network_name, group_name).
Or else you can use friendly_id gem which gives pretty urls.
You can use friendly_id gem. https://github.com/norman/friendly_id
Related
I'm wondering how to generate a url that includes friendly ids for two different models. For example, if you have a post titled 'Rails Tutorial' and belonging to a particular User named 'Michael', and they each use a slug in place of their ID, how would you generate a url that looks like site.com/michael/rails-tutorial.
If you set your route to be:
get ':id/:id' => 'posts#show', as: 'show_post'
the URL will either be michael/michael or rails-tutorial/rails-tutorial, yet the way Friendly_id works from what I know is that your slug is represented by :id, so you can't configure it to work with get ':user_id/:post_id'.
I'm sure I just don't understand FriendlyID enough. Any help is appreciated.
In order to get the url you’re looking for, you’ll need to nest your routes:
resources :users do
resources :posts
end
This will give you the route:
localhost:3000/users/slug/post/slug
You’ll also have to extend FriendlyId in each model, add the slug to each model in a migration and use .friendly in the controller action(s).
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
TL;DR: I want to have username621/posts/title-of-post instead of member/posts/1
The changing of post id to post title was easy enough since I used the freindly_id gem to generate the slugs.
However, I am having difficulty routing to a personalized params route instead of the current namespaced route. Here is the current routing:
namespace :member do
resources :posts
end
I want to replace the member namespace to user's username. So if their username is user123, the route should be user123/posts/title-of-post.
I think that this is not very standard Rails routing and tried looking for similar questions with no results.
for more complicated routes.rb, add a path option
namespace :member, path: ":user_id" do
resources :posts
end
should get what you want, e.g. http://localhost:3000/621/posts/1
then we just have to add friendly_id to User and Post to have it become something like http://localhost:3000/username621/posts/title-of-post
however, you'll need to pore through the codebase for things like member_post_path(post) and change to member_post_path(post.user, post)
Try removing the namespace and adding path option:
resources :posts, path: '/:username/posts/'
Then if you access /username621/posts/title-of-post in your controller you'll see params[:username] = 'username621'
If you have other paths of the form /something/posts add them above this route, otherwise they will be caught by :username.
I have a category with a subcategory and the subcategory has posts. I'd like to link it as following:
/categoryname/subcategoryname/post_id/postname
I've tried doing so by putting this in my routes:
resources :categories do
resources :subcategories do
resources :posts
end
end
But any time I'd like to create a link for my subcategories (/categoryname/subcategory/) via link_to(subcat.name, category_subcategory_path)
I get:
No route matches {:controller=>"subcategories", :action=>"show"} missing required keys: [:category_id, :id]
How would I approach this to get the desired link setup?
Thanks in advance,
Slugs
Firstly, if you're looking to use slugged routes, you'll be best looking at gems including friendly_id or slugalicious -- basically allows you to manage "slugs" for your models -- saving titles or other attributes in URL encoded format
Paths
Secondly, I think you'll resolve your issue by providing values, rather than using the path helper. I would do this:
link_to subcat.name, category_subcategory_path(category.id , subcat.id)
When you use a path helper, it only cares about which params you send. The path helper you're using requires you to set the category_id and subcategory_id params -- which you should pass to the path helper as demonstrated above
This will create the path using id's - if you'd like to use slugs, you'll need to use one of the aforementioned gems (friendly_id is recommended) to set up the slugs in your app
I am trying to make my urls prettier and still use restful resources. I understand that you can override the to_param method if you object has a name property like this:
def to_param
self.name
end
which will give you the route /:model/:name. This is all straightforward, but I have to be capable of having the same name with multiple different languages. I haven't been able to find a blog entry on how to do this, so how can i override the to_param method to provide me a route similar to /:model/:language/:name ?
You could always do:
/language/:language/model/:name
You'd do this with nested routes:
map.resources :languages do |l|
l.resources :profiles
end
Then your route would be:
langauge_profile_url('spanish', #profile)
However...
Depending on what you're trying to do you might be better of using the built in rails i18n stuff. Is this so users can browse the site in different languages??