Rails: URL/path with parameters
get_class_swimming_students_path('2013-01-01', '2013-02-02')
In Rails, URL parameters are mapped to the router in the precise order in which they are passed. Consider the following:
# rake routes
my_route GET /my_route/:first_param/:second_param/:third_param(.:format)
# my_view.html.erb
<%= link_to('My Route', my_route_path('first_param', 'second_param', 'third_param') %>
#=> <a href="/my_route/first_param/second_param/third_param">My Route</a>
Consider also the following case where foo
and bar
are static parameters positioned between dynamic parameters:
# rake routes
my_route GET /my_route/:first_param/foo/:second_param/bar/:third_param(.:format)
# my_view.html.erb
<%= link_to('My Route', my_route_path('first_param', 'second_param', 'third_param') %>
#=> <a href="/my_route/first_param/foo/second_param/bar/third_param">My Route</a>
In the final example, arguments will appear as URL parameters in the order in which they were passed, but not necessarily in the same sequence.
EDIT:
The following syntax is equivalent to the first snippet. The primary difference is that it accepts arguments as named parameters, rather than in the order they're passed:
get_class_swimming_students_path(:start_date => '2013-01-01', :end_date => '2013-02-02')
Add URL params to link_to path
You are doing it the right way apart from a little glitch.
You are really passing the params, but you are not specifying the values yet.
What you need to do is to pass the values of the both the user_id
and the hire_id
, just the way you are passing the amount
.
Something like the following:
<% @foo.map do |f| %>
<b><%= m.reason %></b> at <b>£<%= m.amount_requested %></b>
<span><%= link_to 'a', release_hire_milestone_path(user_id: <value_of_user_id>, hire_id: <value_of_hire_id>, amount: f.bar), class: "tiny button", method: :get %></span> <br/>
<% end %>
Assuming there is the @hire
and @user
objects available for you on the current view... and the user_id
is the @user.id
and hire_id
is the @hire.id
, then the above could be written as follow:
<% @foo.map do |f| %>
<b><%= m.reason %></b> at <b>£<%= m.amount_requested %></b>
<span><%= link_to 'a', release_hire_milestone_path(user_id: @user.id, hire_id: @hire.id, amount: f.bar), class: "tiny button", method: :get %></span> <br/>
<% end %>
Hope this helps.
Match request URL params with rails routes via recognize_path
You can accomplish that with the following snippet:
Rails.application.routes.routes.to_a.reduce(false) do |exists, route|
route.path.to_regexp.match?(path) || exists
end
But I do think the best choice is handling 404 errors using custom logic. Why doing what the router already does for you? Here's an example:
config.exceptions_app = self.routes
# This will route any exceptions caught to your router Rack app. Now you'll want to define routes to display those errors yourself:
# config/routes.rb
get "/404", :to => "errors#not_found"
get "/422", :to => "errors#unacceptable"
get "/500", :to => "errors#internal_error"
(Extracted from http://web.archive.org/web/20141231234828/http://wearestac.com/blog/dynamic-error-pages-in-rails)
Then you can do whatever logic you want to do on ErrorsController
rails route: same url pattern with different parameter
You are going to need to separate the destination method on the controller and update the routes.
I would recommend this approach:
namespace :lawyers do
get 'division/:division/:district' => "profiles#division", as: :division_district_all
get 'speciality/:speciality/:sub_speciality' => "profiles#speciality", as: :speciality_subspeciality_all
end
Update: Based on strong requirements, you could use query params all/:division/:district?query_by=divison
you would only need one route.
get 'all/:primary/:secondary' => "profiles#index", as: :lawyers_all
And then in the controller, manage the logic with something like
def index
case params[:query_by]
when 'division'
# Division logic here
when 'speciality'
# speciality logic here
else
# Error handling here
end
end
Update 2: As you mentioned on the comments, URL cannot change. Still you would need only one route
get 'all/:primary/:secondary' => "profiles#index", as: :lawyers_all
And check existence on db based on the params, this will impact the performance of your app by creating a lot of db requests and also will create the potential issue of matching with the incorrect classes.
def index
if Division.find_by(name: params[:primary]).present?
# Division logic here
elsif Speciality.find_by(name: params[:primary].present?
# speciality logic here
else
# Error handling here
end
end
adding params to a path defined by a string - rails
I did a bit more research into this and found that .to_params gave a one line approach.
<%= link_to "Improve", raw_cml + '?' + { uqr: qid }.to_param, class: "btn btn-pink" %>
Seems to be giving the same results.
I'd be interested in any unforeseen consequences of this method?
Thanks
Pass params into rails path helper
You should pass the extra params in hash syntax
<%= link_to "Pause Assignment", pause_account_complete_assignment_index_path(current_timekeeper: @assignment.current_timekeeper) %>
This will give you params like
<ActionController::Parameters {
"controller"=>"accounts/complete_assignment",
"action"=>"pause",
"current_timekeeper"=>"value_of_current_timekeeper"
} permitted: false>
Related Topics
Single Occurrence Event with Ice_Cube Gem Using Start_Time and End_Time
What Is Purpose of Around_Create Callback in Rails Model
Built in Way to List Directories in a Directory in Ruby
Rspec: How to Write a Test That Expects Certain Output But Doesn't Care About the Method
Rails 4 Strong Parameters Failing When Creating Instances in Rails Console
Ruby Gem Listed, But Won't Load (Gem in User Dir, Not Ruby Dir)
How to Ignore or Skip a Test Method Using Rspec
How to See the Ruby Code in a Proc
How to Tell If I'm Running from Jruby VS. Ruby
Reversing the Order of an Array in Ruby
Rake Db:Migration Not Working on Travis-Ci Build
Ruby on Rails 4 - What Authentication Gem to Use
Accessing Nested Model Attributes Inside a Fields_For Without Using Formbuilder