Rails 3 helper for match route - ruby-on-rails

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

Related

Can Ruby on Rails Routes point a nested URL at a single action?

Is it possible in Ruby on Rails (we're using v2 still) to allow the routes file to map a nested url EG
mydomain.com/controller/object/action
to a single action eg
:controller, :action
?
We currently have a url like
mydomain.com/controller/action
and i want to change it to
mydomain.com/controller/object/action
Thanks in advance
You can write singular route like as follow. You will get record id in params[:id] in controller action.
match 'controller/:id/action' => 'controller#action', via: :get

Ruby on Rails optional route segments

I'm trying to create two routes in my Rails application with optional parameters.
Here's my routes file:
get '(a)(/:area_id/)l/:location_id/(*url_title)', to: 'locations#show', as: :location
get 'a/:area_id/(*url_title)', to: 'areas#show', as: :area
Navigating to the following URLs correctly routes me to the right controller:
http://localhost:3000/a/1/l/2/seo-friendly-title.html
http://localhost:3000/a/1/seo-friendly-title.html
However, navigating to this url does not work:
http://localhost:3000/l/2/seo-friendly-title.html
I receive a No route matches error. Is it possible to make the a/:area_id portion optional when the l/:location_id portion is present?
Using Rails 4.2.4.
You could just add another route...
get 'l/:location_id/(*url_title)', to: 'locations#show'
There's no reason why two routes can't map to the same action.

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Rails 3.2 Routing Error

In my first approach with Rails I have simply create a void SayController and static hello.rhtml view but when the page http://localhost:3000/say/hello started return me a Routing Error like this:
No route matches [GET] "/say/hello"
Try running rake routes for more information on available routes.
Rails version: 3.2.6
Seems like you didn't add a route for hello to your config/routes.rb file.
YourApp::Application.routes.draw do
match 'say/hello' => 'say#hello', :as => :hello
end
This will match route say/hello to controller say (the part before #) and action hello (the part after #).
:as => :hello makes it a named route so you can refer to it as hello_path from within your app.
The error message tells you to run rake routes (from the console) which will show you the existing routes in your app.
You should have something in your config/routes.rb to define that route. Try:
match 'say/hello' => 'say#hello', :as => 'say_hello'
The go to localhost:3000/say/hello
Also check out this documentation:
http://guides.rubyonrails.org/routing.html
I assume, controller: say and action: hello
Add following to config/route.rb
get 'say/hello' => 'Say#hello'

scope in routes.rb not properly working in connection with get

In my rails 3.2.2 app I have the following in my routes.rb:
scope "abc" do
get "hello/index"
end
Which should link "/abc/hello/index" to my index-action in my hello-controller, right?
Instead, I get the error "uninitialized constant Abc"
If I change it to the following
scope "abc" do
match "hello/index", to: "hello#index", via: :get
end
it works just fine.
From my understanding of the routing engine, the two should be the same, shouldn't they?
(See e.g.: http://guides.rubyonrails.org/routing.html#http-verb-constraints )
Also, if you do a "rails g controller hello index" a route named
get "hello/index"
is autocreated suggesting that this is the standard way of doing a non-restful get route.
So why can't I scope such a route? Any ideas?
The examples are using the notation scope "/abc", maybe the initial / is required.

Resources