Migrating Routes to Rails 4 - ruby-on-rails

I am about to migrate my rails 3 application to rails 4.
There are some additional routes on my ressources that make some problems.
I get an error message for this lines in my route file:
resources :teams do
...
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :apply_membership
...
This is the generated error message
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
In rails3 it was possible to define a get and a post route using the same alias and routing them to different controler methods.
Can I do this in rails4, too?
And if yes, how does it have to look like in my route file?

You can not take two route name with same name. but you have done it. so please change,
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :update_membership
Take a look here for rails routings. http://guides.rubyonrails.org/routing.html

Related

Error using alias for routes in Ruby on Rails

I have a route with a namespace
namespace :publishers do
resources :authors
get 'books' :to => 'books'
get 'books/custom_report/:id', :to => "curriculos#custom_report"
end
Often I will have to make links in my application and I know than it`s possible to use a alias for routing like this:
<%= link_to "Books", publishers_books_path %>
I call that publishers_books_path a alias route, does this is the correct name?
Furthermore, I still not able to understand the logic with this alias naming because i can`t use for a new or a custom action like this
link_to 'Show the report', publishers_books_custom_report_path(params[:id])
I'm always get a error of undefined_method for publishers_books_custom_report_path
So there`s some questions
First of all whats it`s the correct name of this feature in RoR?
How I can use the custom_report as aliases to link_to? And also if i need to use some basic operations like new, update, insert?
Can someone give me the link to the documentation to really understant that feature?
First of all whats it`s the correct name of this feature in RoR?
The docs use "path helper" and "named route helpers" interchangeably.
How I can use the custom_report as aliases to link_to?
Use rails route or visit /rails/info/routes in your dev server to get a list of all your routes, their helpers, and controller actions.
Apparently it is publishers_path which doesn't seem right. You can fix this with an as.
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
And also if i need to use some basic operations like new, update,
insert?
A get declares just that one specific route. If you need all the operations on a model, declare it as a resource.
namespace :publishers do
resource :authors
resource :books
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
end
Can someone give me the link to the documentation to really understand that feature?
Rails Routing From The Outside In.

How to correctly define the get and post routes in rails?

Recently I've been working with rails. I have a form with form_tag, which receives a helper post from routes.rb. I have the same route in get to access this form, and I have the route in posrt to be able to send the data of the previous form:
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring_post
My question is, how can I separate these routes? Since, according to investigating, this can cause problems, since both have the same address, and really if it is causing me problems, since when entering this link the form is executed automatically.
Do you necessarily have to separate the two routes? And if that is true, how can I do it?
I tried something similar to what rails do with respect to the method get new and post create method but adapted to my case, but it has not worked for me.
Thank you.
Do you necessarily have to separate the two routes?
No! Unless your both routes have a same name, it won't be a problem. For instance if you have routes like below
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
then Rails would rails an exception like so
ArgumentError: Invalid route name, already in use:
'inscribir_student_monitoring' You may have defined two routes with
the same name using the :as option, or you may be overriding a route
already defined by a resource with the same naming. For the latter,
you can restrict the routes created with resources as explained
here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Since your routes have a different name, this won't be a worry. Though you can shorten your code by using match
match 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', via: [:get, :post]

Error: `Couldn't find Course with 'id'=` when visiting `/courses/show`

I'm new to Rails, and I'm setting up models/controllers for Course and some other models.
When I visit the /courses/show URL in my browser I get the following error:
Couldn't find Course with 'id'=
Screenshot here.
Here's the relevant line from my rake routes and routes.rb:
rake routes
courses_show GET /courses/show(.:format) courses#show
config/routes.rb
get 'courses/show'
You have specified the four routes without any :id parameter, I don't know why you would expect them to have an :id parameter.
I'd recommend that you read the Rails guide on routing and also read the comments in the generated config/routes.rb, in that file you'll see comments like this:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
So, extrapolating that to your example you might end up with:
get 'courses/:id' => 'courses#show'
The example that follows that one shows how to add a named route helper using the :as option:
get 'courses/:id' => 'courses#show', as: :courses_show
Something you'll also see when you read the guide or the comments is that you can use the resources helper to create standard restful routes.

Rails root permalink routing

In Rails, the standard routing to objects is nested to a Model's name, example.com/model/object_id.
Is it anyhow possible to access objects without the Model part, so example.com/object_id shadowly accesses example.com/model/object_id?
Rails includes routes like you said. You can add constraints to determine object_id is integer or string.
get '/:id', to: 'articles#show', constraints: { id: /^\d/ }
This is for more information about routes constraint.
What you are first describing are the RESTful routes provided by the resources template in the rails router.
You can define different routes in the config/routes.rb file.
And for resources, you can provide a path option, where you can define a path.
resources :models, path: "/"
Will provide models resources at the route path. So a GET request to "/" would fire the "models#index" action and "/1/edit" would delegate to "models#edit"

Ruby on rails routes - what does the "as" do? [duplicate]

This question already has answers here:
:as in rails routes.rb
(3 answers)
Closed 8 years ago.
In ruby on rails, what does the "as:" do in route?
Example: http://guides.rubyonrails.org/routing.html 1.2
You can also generate paths and URLs. If the route above is modified to be:
get '/patients/:id', to: 'patients#show', as: 'patient'
and your application contains this code in the controller:
#patient = Patient.find(17)
and this in the corresponding view:
<%= link_to 'Patient Record', patient_path(#patient) %>
In routes as: option is used to make url or path helpers for that particular route. If you look at your route:
get '/patients/:id', to: 'patients#show', as: 'patient'
You have specified as: 'patient' which will enable rails to make patient_path and patient_url helpers
patient_path will give you /patient/:id and patient_url will give you domain/patient/:id
If you run rake routes in your terminal it will list all the routes of your application with there corresponding helper methods. For details checkout path and url helpers
It defines what the route helpers will look like:
patient_path(#patient)
It's the path name which is generated from your routes
For example, if you have the following:
#config/routes.rb
resources :bones, as: :skeleton
You'll get routes as skeleton_path for the bones controller
--
You have to remember that Rails' routing structure is based around "resources" (handled by controllers). Your routes, therefore, should be structured around the controllers your application has
If you want to change the "name" of a particular controller path helper (from bones to skeleton for example), you'll be able to use the as option

Resources