Rails routing forcing param to be present - ruby-on-rails

I'm fairly new to ruby api development and have created the below endpoint in my routes.rb
get "/users/active_users/:since"
However, when the param is not given, I want the param to default to a certain value. How do I enforce this? Also, I want to enforce that param be an integer and not alpha/alpha-numeric. Help is appreciated!

get "/users/active_users/:since"
By making this routes its compulsory to give the params[:since] , other wise it will throw back a error not routes matches
So here by i would suggest you to make routes
get "/users/active_users"
Since it's get type request so its won't affect more, you can append params[:since] in query with routes like this:-
/users/active_users?since=1999
And at your controller you will get params[:since] = 1999
So far as i know it can't be enforce routes to accept only integer params but it can be handle at controller side
params[:since].is_a? Integer
=> true
Or
params[:since].to_i

Related

Rails Routing Error w/ key

So I made this custom Route
on my routes.rb
get'dashboard_report_m/:date/:branch_id'=>'reports#monthly_and_branch'
I'm Getting Routing Error, No route matches [GET] "/dashboard_report_m"
on my rake routes I have this
on rake routes
GET /dashboard(.:format) reports#today_admin
GET /dashboard_report/:date/:branch_id(.:format) reports#date_and_branch
GET /dashboard_report_m/:date/:branch_id(.:format) reports#monthly_and_branch
all the other routes I made are fine but this one just doesn't seem to work.
I tried removing the keys ':date/:branch_id'
and it would work just fine.
I have already made similar routes and they all work just fine except for this one.
Some things to check:
Is there a ReportsController with a ​monthly_and_branch action?
Does the error occur if you visit /dashboard_report_m/2016-09-20/1234 directly or are you using a path helper?
UPDATE
OK so you are accessing the path http://localhost:3000/dashboard_report_m/?date=2016-09&branch_id=1 - you are passing the parameters in as query params, this is not how your route is set up. The way you have it now it is expecting dashboard_report_m/2016-09/1. You need to either remove the date and branch_id params from your route or change the way you access the URL. I suggest reading the Rails Routing from the Outside In guide.
When you access the following route:
localhost:3000/dashboard_report_m/?date=2016-09&branch_id=1
This is a GET request to 'dashboard_report_m', with query parameters: params['date'] = '2016-09' and params['branch_id'] = '1'.
What you should instead be doing is accessing this route:
localhost:3000/dashboard_report_m/2016-09/1
This is a GET request to 'dashboard_report_m/:date/:branch_id' - i.e. using the bound parameters of date and branch_id.
Further reading: Understand the difference between bound parameters and the query string. This is by no means specific to Rails; it's at the core of how all web applications work.

Rails routing sometimes replaces the id in url with id-name. How do I avoid problems?

Generally the url from my report page looks like this:
http://test-account.peter:3000/offices/7/reports/index
However, sometimes it looks like this:
http://test-account.peter:3000/offices/7-peters-office/reports/index
Why does this happen?
It was not really a problem until we changed the controller action from a GET to a POST and renamed it. We had to do this so we could pack more parameters in to the ajax request. Users still have this section of the site bookmarked and it throws errors all day long.
I have tried to redirect the route:
get '/offices/*all/reports/index' => 'offices#show'
get '/offices/:office_id/reports/index' => 'offices#show'
get '/offices/:office_name/reports/index' => 'offices#show'
Is there a way to catch the name? Or do I have to prevent the name from being added to the url in the first place?
In the controller, you would be able to parse the parameter to get just the first character and check if its an integer. However, it would be much better to debug how the parameter is getting assigned to different values and ensure only the id is used. If you're linking to that route in a view, check what is being passed in the link and confirm the value is what you expect it to be.
Rails does routing it does not look in your database for matched data. So without looking at data, your three routes are exactly the same, the variable (office_id & office_name) is just named different. If you get a request on example /offices/:office_name/reports/index, rails will just match the first one since both routes match the request.
You need something in the path that indicates its a name or id. If you will really never have a name and id with the same search, then you could just have one route and try to match a id or name from the DB in the controller.

Rails routing, not a supported controller name error

I am in the process of learning Rails, I am trying to make a small search functionality, I am setting up the route for this like this:
get 'search?q=:keyword' => 'search?q=#show'
and in the url I am trying to access this using
http://localhost:3000/search?q=test
but this is giving me this error: not a supported controller name.
Youssef
The reason is that you are trying to route with the query string ?= still in the path. Rails is a little smarter than that so the parameters will be passed automatically.
get 'search' => 'search#show'
Will retain the parameters in the redirect without you needing to do anything extra.

Custom rails routes not using :id column

I am attempting to create a custom rails route that allows me display information based upon the url. For example, I have products in a database with category_ids and country of origin fields. I would like to be able to type something like /products/(category_id)/canada or something to list items that match that category and country however my attempts have (obviously) been unsuccessful.
So far I've attempted
match 'products/:category_id/:country', to: "products#var_show"
and had no luck.
I've even just tried to make a route that shows the product via the serial code but rails seems to think I'm looking for an id even though I've specified the field in the route and in the controller.
match 'products/:serial', to: "products#show"
Can someone lead me in the right direction and show me what I'm doing incorrect? Thanks.
edit:
Rails seems to make the parameter :id no matter what I call it in the route and controller
Processing by ProductsController#show as HTML
Parameters: {"id"=>"481598745"}
Ideally that would be Parameters : {"serial" => "481598745"} in the second case I asked about.
Try this,
match 'products/:category_id/:country' => 'products#show',:as => :show

Rails 3 gives routing error with point in URL

I have a search form written with Rails 3 when I query it everything works fine as long as I do not put a point in my query. Eg:
http://localhost:3000/en/job/search/q/test - WORKS
http://localhost:3000/en/job/search/q/test. - DOES NOT WORK
URL with point at the end gives a
Routing Error: No route matches [GET] "/en/job/search/q/test.
Does anybody know how I can solve this? Thanks.
By default, Rails interprets everything to the right of the decimal as the format. You need to set the :constraints
Here is a good article on the subject: http://coding-journal.com/rails-3-routing-parameters-with-dots/
Here is the reference in the Rails API that should help you resolve your issue:
http://guides.rubyonrails.org/routing.html#specifying-constraints
http://guides.rubyonrails.org/routing.html#dynamic-segments
Since your passing a string in the search as a get request, you might also consider route globbing: http://guides.rubyonrails.org/routing.html#route-globbing
Your route would be something like this:
match ":language/job/search/*query"
and in your controller, you would get the value from the route using the params[] array:
q = params[:query]
Be sure to use best practices when passing this to ActiveRecord to avoid a SQL injection attack.
What #iltempo said.
Also, it would be a good idea to switch your search from using GET requests to POST requests to make all these problems go away.

Resources