Rails routes: GET without param :id
Resource routes are designed to work this way. If you want something different, design it yourself, like this.
match 'users/me' => 'users#me', :via => :get
Put it outside of your resources :users
block
Why redirecting to #show action without passing the :id param work (in rails)?
I found this in Rails source:
Missing routes keys may be filled in from the current request's
parameters (e.g. +:controller+, +:action+, +:id+ and any other
parameters that are placed in the path).
So here :id
exists in the current request params
and it's used for this route.
Rails 3 Routes with or without :id
perhaps if you use the optional parameter as
# Routes
match 'product_specs/(:id)' => 'home#product_specs'
# Controller
def product_specs
if params[:id].nil?
product_specs = ProductSpecs.all()
else
product_specs = ProductSpecs.find(params[:id])
end
Would something like that work?
Rails routes with :name instead of :id url parameters
params
The bottom line is you're looking at the wrong solution - the params
hash keys are rather irrelevant, you need to be able to use the data contained inside them more effectively.
Your routes will be constructed as:
#config/routes.rb
resources :controller #-> domain.com/controller/:id
This means if you request this route: domain.com/controller/your_resource
, the params[:id]
hash value will be your_resource
(doesn't matter if it's called params[:name]
or params[:id]
)
--
friendly_id
The reason you have several answers recommending friendly_id
is because this overrides the find
method of ActiveRecord
, allowing you to use a slug
in your query:
#app/models/model.rb
Class Model < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
end
This allows you to do this:
#app/controllers/your_controller.rb
def show
@model = Model.find params[:id] #-> this can be the "name" of your record, or "id"
end
How do I call my show method without any parameters?
You can't, at least when you use resources
— resources
generates a standard group of routes that adhere to RESTful practices. In your case, it's generating get '/votes/:id', to: 'votes#show'
Because there is no matching param, it assumes that you're trying to go to votes#index
If you want to match both, I would do something like this in your routes.rb
: get '/votes(/:id)', to: 'votes#show'
the parens denote an optional param. You may need to put this prior to your resources
block, or change your resources
block to omit the show
action.
More on params: http://guides.rubyonrails.org/v5.1.1/routing.html#bound-parameters
Ruby on Rails routes - Query parameter vs id
Because you have a dynamic segment in the same position as :id
you have to use constraints on your route.
https://guides.rubyonrails.org/routing.html#segment-constraints
# `restaurants/1` will be ignored
# `restaurants/anything-else` will be routed to RestaurantsController#index
get 'restaurants/:category',
to: 'restaurants#index',
constraints: { category: /[^0-9]+/ }
resources :restaurants
Alternate route to `create` - route without id fails
Try
resources :users do
post :new, on: :collection
end
You need to specify the HTTP verb, action and the target. If the target is :member
, then Rails expects an "id" parameter and constructs the URL based on that.
Make sure to read this guide on Rails routes.
Rails 3 resource routing without an id
You can use to_param to craft the rails uses
class Post < ActiveRecord::Base
...
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
end
More info: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-to_param and http://www.seoonrails.com/to_param-for-better-looking-urls.html
If you go this route, you'll want to create a permalink attribute, and use Post.find_by_permalink(params[:id]
) rather than Post.find(params[:id])
i have to put a parameter in routes.rb
Try to change this on routes.rb
get 'api/forecast/service_name:' , to: 'api/forecasts#index'
Related Topics
How to Do "Late" String Interpolation in Ruby
Access Ruby Hash Using Dotted Path Key String
How to Connect to Browser Using Ruby Selenium Webdriver
Ruby: Differencebetween the Comparatives: "||" and "Or"
Ruby Ssl with Twitter Failed on Cert Openssl Issue on Windows 7
Fastercsv Error with Ruby 1.9.2
How to Perform Vector Addition in Ruby
Can Someone Please Explain Class << Self to Me
How to Create a Charge and a Customer in Stripe (Rails)
Rails Render of Partial and Layout in Controller
Ruby: How to Write a Bang Method, Like Map
How to Run All Ruby Scripts with Warnings
How to Force Rails to Load All Models
Disable Rdoc and Ri Generation by Default for Rubygems 1.8.X