Resource not generating helper method for create - ruby-on-rails

I have a model called package, which has a controller package created under a name space admin.
so my resource in my routes is declared as below
namespace :admin do
resources :package
end
when I run rake routes
admin_package_index GET /admin/package(.:format) admin/package#index
POST /admin/package(.:format) admin/package#create
new_admin_package GET /admin/package/new(.:format) admin/package#new
edit_admin_package GET /admin/package/:id/edit(.:format) admin/package#edit
admin_package GET /admin/package/:id(.:format) admin/package#show
PUT /admin/package/:id(.:format) admin/package#update
DELETE /admin/package/:id(.:format) admin/package#destroy
If you see there is no helper method generated for create, which should have been admin_packages_path
controller
#newpackage = Package.new
view.html.erb
form_for [:admin,#newpackage] do |f|
end
is reporting that it is not able to find the admin_packages_path. Can somebody please explain how should we declare this in routes to generate the proper helper method for create ?

Your routes are named incorrectly, you're pairing a plural (resources) with a singular (:package).
If you will be working with multiple packages, you should declare resources :packages - this will generate you correctly named routes for all seven RESTful actions (index, show, new, create, edit, update, destroy).
If you are only working with a single package, you will need to specify the URL as an option for your form manually, eg.
form_for [:admin, #newpackage], url: admin_package_path do |f|

You really need to changes the routes to following:
namespace :admin do
resources :packages
end

Related

Failed to try to use 2 ids on nestes routes in rails

I'm trying to pass 2 ids to the controller within the update action, but it does not stop, I do not recognize the first id, student_id. This is the definition of the route within routes.rb
post 'registers/students/:student_id/notes/:note_id/edit', to: 'registers/students/notes#update', as: :update_registers_student_note
While this is the part of the form_for, using the corresponding helper
<%= form_for #note, url: update_registers_student_note_path(:student_id,:note_id), method: :post do |f| %>
My question is how can I correctly pass the 2 ids of the corresponding resources, since the form only recognizes me note_id, and not student_id
Thank you
The helper should works. You also can use update_registers_student_note_path(student_id: 1, note_id: 12).
But I suggest you to use nested resources in your routes instead.
You can write somethings like that:
namespace :registers do
resources :students do
resources :notes
end
end
The new url helper will be registers_student_note_path(<note_id>, student_id: <student_id>) with method PATCH.
yes, you did put a right thing for url_helpers. You can try to open rails console and try it with these command
include Rails.application.routes.url_helpers
update_registers_student_note_path(1,2)
you will see => "/registers/students/1/notes/2/edit"

Rails form builder with admin namespace

The code below works well to create new transactions on the Invoice show view. It however doesn't work when in admin namespace. i.e /admin/invoices/1/ but works on /invoices/1/
show.html.erb
<%= form_for([#invoice, #invoice.transactions.build]) do |form| %>
....
transactions form input
routes.rb
resources :invoices do
resources :transactions
end
When calling form_for in a namespace route like /admin/invoices/1/, Rails will automatically append admin to your route. In other words, form_for([#invoice, #invoice.transactions.build]) would POST to a route like /admin/invoice/:id/transactions/ rather than /invoice/:id/transactions/.
To fix, explicitly set the URL of the form and use a route helper method to infer the correct route:
form_for(#invoice, url: invoice_transaction_url(#invoice.id))
Note that you may need to replace invoice_transaction_url with the correct route. Use rake routes to find the helper method that corresponds to the desired controller POST action.

What is the purpose of adding routes for additional new actions?

The guide Rails Routing from the Outside In has a section named Adding Routes for Additional New Actions in chapter 2, Resource Routing: the Rails Default, with this example:
resources :comments do
get 'preview', on: :new
end
which generates the following route:
Prefix Verb URI Pattern Controller#Action
preview_new_comment GET /comments/new/preview(.:format) comments#preview
The same result can be achieved at least in two other ways:
1) Adding a collection route inside resources :comments like get 'new/preview', to: :preview, on: :collection (if you don't bother about the route name)
2) Adding just this route to the corresponding singular resource:
resource :comment, only: :preview do
get 'new/preview', to: :preview, as: 'preview_new', on: :collection
end
which has exactly the same behavior.
So why there is a on: :new option? Is it just a shortcut? If it is, why it's just for 'new actions' and there are not any similar options for the other default REST actions, like on: :edit or on: :delete?
This is a shortcut that is unique to the RESTful action new, in which an object is instantiated but not yet persisted.
For example, if you are filling out a form for a new object, you would provide yourself with a link_to "preview", which sends all of the attributes to your preview action (instead of directly to create). This is similar to a show view, but for an object that only exists in memory.
This functionality isn't logical for destroy, update, index, show or create. I suppose you could use a preview on an edit action after assigning new attributes before sending to update, but you'd have to create the route manually.
Actually there is a subtle difference.
If you add it to the resources :comments the path helper generated has a pluralized resource name so it will be preview_new_comments and the path will be /comments/new/preview. If you add it to the corresponding singular resource it doesn't pluralize the resource name in the path so you get /comment/new/preview but the helper will be preview_new_comment. Using :on will generate the path helper with the singular resource preview_new_comment but the route will have the pluralized resource /comments/new/preview.
To get the same behaviour you could use the following more verbose route outside the resource
get 'collections/new/preview', to: 'collections#preview', as: :preview_new_comment, on: :collection
I'm not sure why there is no on: :<action other than new> option. They advise against having too many new actions in the documentation but if you are going to have a new 'new' it seems a reasonable possibility you might need an new 'edit' to edit your new 'new'. However, things become more complicated because to edit you would need a path like collections/<id>/edit/preview and the path helper of the solution above stops working as you might expect. More importantly at this stage maybe you start veering away from the original intention of the option.

Including attributes in custom Rails routes

I hope the title is not to misleading, as I don't know a better title for the problem I'm working on:
I have a doctor which belongs to location and specialty. I'd like to route to show action of the doc controller like this:
/dentist/berlin/7
I defined my routes like this:
get ':specialty/:location/:id', to: 'docs#show'
And in my views create the following url to link to the show action of the doc controller:
<%= link_to doc.name, "#{doc.specialty.name}/#{doc.location.name}/#{doc.id}" %>
Is this a good solution to the problem? If not, is there a cleaner way to construct urls like this possibly using resources? What the heck is the name for a this problem?
Thank your very much for your help in advance.
For references, you should have a look at this page (especially the end of section 2.6)
If it is only for a single route, it's okay as you did. But then if you want to have more than one route (like /dentist/berlin/7, /dentist/berlin/7/make_appointment, etc.) you might want to structure a bit more your routes so as to take advantage of rails resources.
For example, instead of
get ':specialty/:location/:id', to: 'doctors#show'
get ':specialty/:location/:id/appointment', to: 'doctors#new_appointment'
post ':specialty/:location/:id/appointment', to: 'doctors#post_appointment'
You could have something like this (the code is almost equivalent, see explanation below)
resources :doctors, path: '/:specialty/:location', only: [:show] do
member do
get 'new_appointment'
post 'create_appointment'
end
end
Explanation
resources will generate the RESTful routes (index, show, edit, new, create, destroy) for the specified controller (doctors_controller I assume)
The 'only' means you don't want to add all the RESTful routes, just the ones specified
Then you want to add member actions, ie. actions that can be executed on a particular item of the collection. You can chose different syntaxes
resources :doctors do
member do
# Everything here will have the prefix /:id so the action applies to a particular item
end
end
# OR
resources :doctors do
get 'new_appointement', on: :member
end
By default, the controller action is the same as the path name you give, but you can also override it
member do
get 'appointment', action: 'new_appointment'
post 'appointment', action: 'post_appointment'
end
Rails has some wonderful helpers when it comes to routing !
The correct approach is to give your route a name, like this:
get ':specialty/:location/:id', to: 'docs#show', as: 'docs_show'
Then you can use it like this:
<%= link_to doc.name, docs_show_path(doc.specialty.name, doc.location.name, doc.id) %>
Note 1:
Rails appends _path at the end of the route names you define.
Note 2:
You can see all the available named routes by executing rake routes.

How do I specify destroy path helper in a view of my Rails app?

I have a model Category in my Rails app. According to rails RESTful Routes, I can perform CRUD Operations on model, having resources: categories defined in my routes.rb.
But how do i define destroy path helper in my view to perform DELETE action, just like edit_category_path(#category) to edit the record. I tried like this
destroy_category_path(#category) but getting error as
undefined method `destroy_category_path' for #<#<Class:0x00000005371298>:0x000000053734f8>
The path is exactly the same as the show action ('/categories/:id'), but you also need to specify the DELETE HTTP method:
button_to #category, method: :delete
Note, it is not considered safe to use links having destructive/constructive actions, as those might be visited by robots.
There is a path helper for delete, but Rails defaults to having this route not defined. To activate the helper you need to add delete to your resourceful routes in your routes.rb file.
resources :categories do
member do
get :delete
end
end
Once you've done that you should be able to use delete_category_path(#category).
Then inside your category you can call destroy on the object from your delete action.

Resources