Route with multiple paramaters - ruby-on-rails

I have the route:
match "/invite/create/:first_name/:last_name/:email/:phone" => 'invite#create'
and when I try:
http://localhost:3000/invite/create/bill/bob/bob#gmail.com/1234567890
I get a No route matches [GET] "/invite/create/bill/bob/bob#gmail.com/1234567890" error.
Whats wrong with my route?

If you have other routes that start with the same url fx /invite, then is it important that you define match "/invite/create/:first_name/:last_name/:email/:phone" => 'invite#create' first.
However this is not a very good practice, you should always do a post request when creating new records.

Related

How to restrict the wild card route

get '/:company' => 'organizations#show', as: 'company_home'
I have this route, this is a way so that different companies registered to my application will login. As I have overridden all the devise related things. This is working fine until I have realized that for every route, this is getting applied
When I hit
get 'employee_dashboard' => 'dashboard#show'
The Parameters: {"company"=>"employee_dashboard"} are going to organizations#show
But I want it to hit dashboard#show how to get around with this?
Move the wildcard route after
get 'employee_dashboard' => 'dashboard#show'
or at the end of route file
get '/:company' => 'organizations#show', as: 'company_home'
so this route will be used only if no other route matches

Rails 3 helper for match route

In a rails 3 application following match route is defined in routes.rb
match 'accounts/:account_type/:account_id/edit_account' => 'accounts#edit_account'
In controller i redirect to this URL
redirect_to "/accounts/account/#{account_id.to_s}/edit_account"
It works fine but i need a route helper instead of manually building the URL something like this
edit_account_accounts_path(account_id: id, account_type: 'some_type')
Is there any way to do this ?
you can try
match 'accounts/:account_type/:account_id/edit_account', to: 'accounts#edit_account', as: 'edit_account_accounts'
for more help see The Lowdown on Routes in Rails 3

Ruby on Rails - Regular expression for error routes

How do I define a routes match any things exclude string ( like 'websocket' )?
Thanks!
Based on the comments, it sounds like you want to match /websocket to a specific action and everything else to an 404 error page.
Utilizing the fact that routes are matched in the order they are defined in routes.rb, this is a good approach to do it:
match '/websocket' => 'controller#action'
match '/:slug' => "errors#show", :code => 404, :via => [:get]
When a request /string comes, the routing subsystem will first try to match it to the first line, and if string is equal to websocket then the match is successful and no more routes will be matched.
If string is not websocket on the other hand, then it will match the second line.

Why did Ruby on Rails' URL Helper put a period in my URL?

I have the following code in my view (RoR 4):
tbody
- #order_submissions.each do |order_submission|
tr
td = order_submission.id
td.table-actions
span = link_to "Show", order_submissions_path(order_submission.id)
td = order_submission.id
successfully displays as the ID number (533ab7337764690d6d000000)
But...
order_submissions_path(order_submission.id)
Creates a URL that comes out as:
order_submissions.533ab7337764690d6d000000
I want it to be
order_submissions/533ab7337764690d6d000000
Where did that period come from?
This is my route:
get 'order_submissions/:id' => 'order_submissions#show'
And when I run rake routes I get:
GET /order_submissions/:id(.:format) order_submissions#show
The (.:format) is probably what's messing it up but I don't know why. I just want it to put a slash in there.
If I change my code to this it fixes it:
span = link_to "Show", order_submissions_path + '/' + order_submission.id
But that's a really, really stupid workaround.
EDIT: Here are my routes:
get 'order_submissions' => 'order_submissions#index'
get 'order_submissions/new' => 'order_submissions#new'
post 'order_submissions' => 'order_submissions#create'
get 'order_submissions/:id' => 'order_submissions#show'
get 'order_submissions/:id/edit' => 'order_submissions#edit'
patch 'order_submissions/:id' => 'order_submissions#update'
get 'order_submissions/:id/delete' => 'order_submissions#delete'
delete 'order_submissions/:id' => 'order_submissions#destroy'
The order_submissions_path (plural) points to /order_submissions. It takes two arguments, the first being the format for the request (e.g. html). Your ID is being passed in for this argument, leading to the resulting URL you're seeing.
You actually want the singular path helper, order_submission_path, which accepts an ID as the first argument.
Because it should be a singular form:
order_submission_path(order_submission.id)
Not
order_submissions_path(order_submission.id)
order_submissions_path points onto index action. You can also remove id from the end.
UPDATE:
Just notice you route file. Do you have any resources defined there? The route you posted wouldn't generate any url_helper as you dind't specify route name (most likely this entry is obsolete, as I expect there is resources :order_submissions somewhere there as well).
You don't get a named route by default. The route you showed from rake routes doesn't list a named route, for example.
GET /order_submissions/:id(.:format) order_submissions#show
Normally, you'd see the named route in front of GET there.
So you can define it yourself and then your route will work:
get 'order_submissions/:id' => 'order_submissions#show', as: :order_submission
Notice the as: :order_submission bit. Now, order_submission_path(order_submission.id) will work. (Note: .id is superfluous if your order_submission responds to to_path and returns id.)
I'm guessing you have another route in your rake routes output that uses the named route you supplied and that doesn't use /:id. Perhaps your index route?

Rails routing send string with dots and int

I'm new to rails...
I try to send such string as :search param: "55.675155, 21.833466" and 2 as :id param...
But something is bad...
i get No route matches [GET] "/exchanger_lists/get_exchangers_via_coordinates/.....
My route file:
match 'exchanger_lists/get_exchangers_via_coordinates/:search,:id' => 'exchanger_lists#get_exchangers_via_coordinates'
But also how then url must look in browser???
How to do this in Rails way? I read doc's, but something is not clear on 100% (
Just how to configure my route and how to call from browser?
match 'exchanger_lists/get_exchangers_via_coordinates/:search,:id' => 'exchanger_lists#get_exchangers_via_coordinates',
constraints: { search: /[^\/]+/ }
From here
You could change your routes like following:
match 'exchanger_lists/get_exchangers_via_coordinates/:x/:y/:id' => 'exchanger_lists#get_exchangers_via_coordinates'
params[:x] and params[:y] will hold your coordinates. I think this is more beautiful code, than holding the to coordinates in one param.

Resources