I'm new to Rails and I'm having issues setting up non-resourceful routes with querystring parameters. I am trying to access the following URL:
http://localhost:3000/exerciseLogs?userID=1
When I access this URL, I receive the following error:
No route matches [GET] "/exerciseLogs"
My routes file is defined as follows:
get 'exerciseLogs?userID=:user_id' => 'exercise_logs#index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
get 'exerciseLogs/:id' => 'exercise_logs#show', :defaults => { :format => 'json' }, :as => :get_exercise_log
post 'exerciseLogs?userID=:user_id' => "exercise_logs#create", :defaults => { :format => 'json'}, :as => :create_exercise_log
patch 'exerciseLogs/:id' => 'exercise_logs#update', :defaults => { :format => 'json' }, :as => :update_exercise_log_patch
put 'exerciseLogs/:id' => 'exercise_logs#update', :defaults => { :format => 'json' }, :as => :update_exercise_log_put
delete 'exerciseLogs/:id' => 'exercise_logs#destroy', :defaults => { :format => 'json' }, :as => :delete_exercise_log
What is the best way to solve this problem so that my routes actually go the controller?
You don't have to define query string parameters in your route. For details checkout rails routes with query string. So in your case you can do something like this:
get '/exerciseLogs' => 'exercise_logs#index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
Define route like this
get 'exerciseLogs/user/:user_id' => 'exercise_logs#user_log_index', :defaults => { :format => 'json'}, :as => :get_user_exercise_logs
and then access it
http://localhost:3000/exerciseLogs/user/1
Define seperate action user_log_index in controller
Related
I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL
Have and old rails 3 app with a routes.rb like this
RailsAppli::Application.routes.draw do
root :to => "landing#pos", :constraints => { :host => "pos.com.ar" }
root :to => "landing#desa", :constraints => { :host => "desa.com.ar" }
root :to => "landing#plan", :constraints => { :host => "dise.com.ar" }
thats work fine but i upgraded to rails 4 and
Invalid route name, already in use: 'root' (ArgumentError)
Whats the problem.
Thanks.
Updating answer based on: Separate Domain for Namespaced Routes in Rails 4
Shortened:
1) define a custom constraint class in lib/domain_constraint.rb:
class DomainConstraint
def initialize(domain)
#domains = [domain].flatten
end
def matches?(request)
#domains.include? request.domain
end
end
2) use the class in your routes with the new block syntax
constraints DomainConstraint.new('mydomain.com') do
root :to => 'mydomain#index'
end
root :to => 'main#index'
or the old-fashioned option syntax
root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')
Done!.
Only one root.
then
RailsAppli::Application.routes.draw do
get '/', :to => "landing#pos", :constraints => { :host => "pos.com.ar" }
get '/', :to => "landing#desa", :constraints => { :host => "desa.com.ar" }
get '/', :to => "landing#plan", :constraints => { :host => "dise.com.ar" }
Thanks
I am upgrading application from rails 2.3/x to 3.2.x
I have a old route like this:
map.calendar '/calendar/:year/:month', :controller => 'calendar', :action => 'index', :year => Time.now.year, :month => Time.now.month
rake routes generate something like this:
calendar /calendar/:year/:month {:controller=>"calendar", :action=>"index"}
I am not sure what is year and month here, are they default values being sent in case none is given? What would be its rails 3.2.x syntax.
I tried something like this:
match '/calendar/:year/:month' => 'calendar#index', :defaults => {:year => Time.now.year, :month => Time.now.month}
The route generated in rails 3.2.x is:
/calendar/:year/:month(.:format) calendar#index {:year=>2013, :month=>9}
Help me out to convert this route correctly.
You should add as key to your match call:
match '/calendar/:year/:month' => 'calendar#index', :defaults => {:year => Time.now.year, :month => Time.now.month}, :as => 'calendar'
Hi I'm trying to change my routes from a rails 2 project to match the syntax in rails 3. How would I change this:
map.connect "/stylesheets/:action.css",
:controller => "stylesheets",
:format => "css"
so that the :action can be matched to an action from the stylesheets controller?
Would it be something like this?
match 'stylesheets/:action.css', :to => "stylesheets#{:action}"
Also, what is the syntax for :format in the routes for rails 3?
You're almost there with your solution. The only thing you have to change is the way you reference :action in the :to value.
match 'stylesheets/:action.css', :to => 'stylesheets#:action', :format => :css
As you can see, the syntax for :format didn't change.
I think it would be smth like:
match 'stylesheets/:action', :controller => :stylesheets, :defaults => { :format => :css }
or you can constraint your routes to .css format using :constraints => { :format => 'json' }
I recommend you to read through http://guides.rubyonrails.org/routing.html
My question it's about use a filter by date in the route of a rails application, at the moment I all ready have the rule that match the pattern of the date in the routes.rb file, it's looks like this:
match "itineraries/:day/:month/:year" => "itineraries#index",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
match "itineraries/new/:day/:month/:year" => "itineraries#new",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
resources :itineraries
match '/:controller(/:action(/:id))'
root :to => "itineraries#index"
That match for example /itineraries/01/01/2011, the problem comes when I generate a route from the resource, for example, itineraries_path(:year=>2011,:month=>1,:day=>1) generate:
/itineraries?day=1&month=1&year2011
instead of
/itineraries/01/01/2011
Is there a way to put the match rule inside the resource mapping?
well, short answer is your route does exists, but it's not named yet (check rake routes to convince yourself) and that's why you can't call it just like that (using itineraries_path)
so, the correct way to do it would be for example:
match "itineraries/:day/:month/:year" => "itineraries#index",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
:as => "itineraries_date"
(notice the :as part)
now, if you call itineraries_date_path(11,12,1998) it will give you itineraries/11/12/1998