Rails: Specific Name + ID in URL - ruby-on-rails

My problem is that I want a url like /product/title-1 but I only get /product/1-title!
Here is my code in product.rb:
def to_param
"#{id}-#{title.parameterize}"
end
routes.rb:
resources :products do
resources :comments
end
What I must change to get it?

Related

Rails change path of resource

I have this routes.rb:
Rails.application.routes.draw do
resources :students do
collection do
get :posters
get :stories
end
end
root "department#index"
get "department/about"
end
Initially, students is a database where I store students information. So for each student the URL should be students/:id. Now I want to move it to students/stories/:id.
How can I do it? Thank you!
You basically need to define a students namespace and a stories resource inside it like below:
Rails.application.routes.draw do
namespace :students do
resources :stories, only: %i[index show update destroy create] do
end
end
end
I have added all the default actions in the only list. You can remove the routes you don't need.

Rails 4 nested routes

routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
resources :states do
resources :cities do
get 'listings'
end
end
end
I am looking to have my GET URL set up like:
../state.id/city.id/listings.id
I am using friendly_id so the urls will read like:
../OR/Portland/2011-ford-truck
Listing is it's own model (resource) too in this case. You will also need a resources for listing. If it only has a show action, you can limit it like this:
resources :states do
resources :cities do
resources :listings, only: [:show]
end
end

rails routing nested resources duplicate

I want to support:
POST images/1/comments/2/like
and
POST comments/2/like
They both point to same resources with same action. How can I do that in rails route file?
resources :images do
resources :comments do
member do
post 'like'
end
end
end
This will work for
POST images/1/comments/2/like
but how can I also for when I don't specify the images/1 part?
You can make it more beautiful actually. According to http://ruby-journal.com/how-to-dry-your-rails-routes/, this also works:
comments = Proc.new do
member do
post 'like'
end
end
resources :comments, &comments
resources :images do
comments.call
end
and in Rails 4 you could use concerns
concern :comments_concern do
member do
post 'like'
end
end
resources :comments, concerns: :comments_concern
resources :images, concerns: :comments_concern do
#do more stuff here
end
I didn't test this, but it might help. Have a look at the website mentioned. Good luck
-frbl
I would consider reworking which RESTful routes for Comment you're nesting, and which you're not. I'm assuming your models look something like this:
# app/models/image.rb
class Image < ActiveRecord::Base
has_many :comments
end
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :image
end
Because your Image and Comment models possess a one-to-many relationship, I can see why you'd think to nest the comments resource route within the images one. However, of all the CRUD actions in comments_controller.rb, only create actually requires that a parent image ID be explicitly passed in. From a RESTful perspective, only the new and create actions require that a image_id is passed to the action. The edit, update, delete, and like actions can all take place independently of the parent image.
Consider the alternative routing schematic instead:
# config/routes.rb
resources :images do
resources :comments, :only => [:index, :new, :create]
end
resources :comments, :only => [:show, :edit, :update, :destroy] do
member do
post 'like'
end
end
Now, only the comment actions that are explicitly depended on a parent ID are actually nested within the images routes. The remainder of the comment actions are directly routed to the comments controller without passing in the parent ID. Your routes are no longer duplicated, and each action will have exactly one route declared for it.
simply add resources :images it may work
Not sure there is a much more beautiful way than repeating this below:
resources :comments do
member do
post 'like'
end
end
Like so:
resources :images do
resources :comments do
member do
post 'like'
end
end
end
resources :comments do
member do
post 'like'
end
end

Adding a Route to the base URL of plural Resources

In Ruby on Rails, is there a way to add another RESTful action to the base URL of a plural resource? I'm looking for something like this:
resources :groups do
resources :users do
put on: :base, to: 'users#update_all'
end
end
Which would generate the route: [PUT] groups/:group_id/users => users#update_all
I've already tried doing this:
resources :groups do
resources :users
put 'users', on: :member, to: 'users#update_all'
end
But that doesn't preserve the value of params[:group_id] in the controller.
resources :users do
collection do
put '' => 'users#update_all' ## PUT /users
end
end
UPDATE
It would be recommended to do this though:
resources :users do
collection do
put 'update_all' ## PUT /users/update_all
end
end
Both route to the update_all action of the users controller.
RESOURCES
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Custom Nested Routes

I am trying to get custom routes based on the username rather than the ID. I have it working to get to the show page of the user but I am also trying to nest the resources so that I can see his posts and comments using the same syntax.
Example:
Works... "mysite.com/users/username/"
Does not work... "mysite.com/users/username/posts/"
routes.rb
...
# Users with the Username...
match 'users/:username' => "users#show" do
get :posts
get :comments
end
# Users with the ID...
resources :users do
get :posts
get :comments
end
...
Perhaps you can use the to_param method and update your nested routes/resources:
routes.rb:
resources :users do
resources :posts
resources :comments
end
user.rb
class User < ActiveRecord::Base
def to_param
username
end
end
..for finds in the UserController:
#user = User.find_by_username(params[:id])
(or any variation of finding by the username criteria)

Resources