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.
Related
Given a path such as
/foos/123
and a route
get '/foos/:id', as: 'foos'
How to get the ID using Rails routes (reverse) look up?
In this example, path.split('/').last would work, and a regex would be better. But how to use the Rails routes to do it?
This functionality was provided by Rails.application.routes.recognize_path but has been deprecated.
Note: This not part of a controller. Please do not answer about how to use routes within a controller.
Rails 6.
You can't. Routing is actually a lot more complicated than that.
The routes are not just a simple static set of regexes that match a string to a controller and action. You need an entire request object. You have to remember that constraints put things like headers, cookies and even middleware like Warden in the picture.
Rails.application.routes.recognize_path was depreciated since it completely failed at handling this complexity. It just gave a false sense of simplicity which is probably the worst thing software can do.
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
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.
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.
Just as you figure out the route when the browser hits the webpage in Rails, how would you resolve it on the server side?
For example I want to return a URL to a RESTful resource called Bookmark in an API call and want to return the 'show' action of it, and I know that:
Bookmark id: 12
Then I want to resolve it to a string:
'/bookmarks/edit/12'
so that I can get this from my Model for example.
How would I go about doing this?
Thanks!
Pretty much everywhere in the views/controllers you can use route helpers to DRY up route references.
In models, you'll need to explicitly call the route helper like so.
Rails.application.routes.url_helpers.edit_bookmark_path(id) # => '/bookmarks/12/edit'
When using the default resourceful route generator method in routes.rb like
resource :bookmarks
I'm not sure I understand - your server is the thing that's making all of those routes work - the client's browser isn't figuring out what the route is - you application is doing it.
The paths to your resources are available as helper methods at all times (at least within the controllers, and views). As such, you should return the string as the body of a response in one of your actions, in the controller that's handling your API calls.
Check your rake routes on the command line, and you'll see a list of them. In the case of your example above, it would likely be called edit_bookmark_path(12)