I keep getting the following error. I'm trying to develop my own blogging platform as a way for me to learn and get better.
Error:
Showing /home/ubuntu/workspace/app/views/posts/show.html.erb where
line #36 raised:
undefined method `post_comments_path' for
<#:0x007f88f0044248> Did you mean? posts_path
app/views/posts/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
config/routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :posts
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root :to => "posts#index"
end
You do not have a comments resource nested in a posts resource (or at all) so you cannot create URLs using a comment within a post.
You need to define your comments routes before you can generate URLs for them.
If you want to nest them within posts, you should modify your current resources :posts line:
resources :posts do
resources :comments
end
resources in your routes files generate helper methods that Rails uses to produce URLs for models, in this case, post_comments_path.
You probably need to update it to look like
routes.rb
resources :posts do
resources :comments
end
after doing so you should be able see the new routes using rake routes or rails routes in the console.
You have to edit following changes to routes.rb file and try again
resources :posts do
resources :comments
end
Your route file is not correct. Place below code in your routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :posts do
resources :comments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root :to => "posts#index"
end
Related
If a have 2 nested routes: post/comments, the form_for is like this (in the _form file):
form_for([#post, #post.comments.build])
But in this case I have 3 nested routes: class/post/comments, and I don't know how to code it in the _form file.
Or there is other alternative to nest 3 routes?
config/routes.rb
Rails.application.routes.draw do
resources :campus do
resources :salas
end
devise_for :users
resources :cursos do
resources :publicacions do
resources :comentarios
end
end
get 'welcome/index'
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
curso_publicacion_comentarios_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios(.:format)
comentarios#index
POST /cursos/:curso_id/publicacions/:publicacion_id/comentarios(.:format)
comentarios#create
new_curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/new(.:format)
comentarios#new
edit_curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id/edit(.:format)
comentarios#edit
curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#show
PATCH /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#update
PUT /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#update
DELETE /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#destroy
Just follow the same pattern:
form_for([#class, #post, #comment])
But be advised that you have some problems with you code:
Don't use class to name variables, association and so, because it is used by Ruby and it will drive you into trouble.
Do not nest routes too much. As Rails guide says, nesting more than 1 level should be avoided.
Do not initialize objects in the form definition. Do it in the controller action or you won't be able to display validation error.
This may not make sense but I'm trying to learn harder stuff and progress, it seems like I'm missing the ID for address but can't seem to find a solution.
I included the url in form_for because when I remove it, the app breaks. But seems like I predefined the url than edit breaks.
<%= form_for([#address.user, #address], :url => user_addresses_path) do |f| %>
Error Readout:
No route matches [PATCH] "/users/1/addresses"
When I remove :url=>
undefined method `user_client_address_path'
routes.rb
Rails.application.routes.draw do
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
rake routes partial output (let me know if more is needed)
user_addresses GET /users/:user_id/addresses(.:format) client/addresses#index
POST /users/:user_id/addresses(.:format) client/addresses#create
new_user_address GET /users/:user_id/addresses/new(.:format) client/addresses#new
edit_user_address GET /users/:user_id/addresses/:id/edit(.:format) client/addresses#edit
user_address GET /users/:user_id/addresses/:id(.:format) client/addresses#show
PATCH /users/:user_id/addresses/:id(.:format) client/addresses#update
PUT /users/:user_id/addresses/:id(.:format) client/addresses#update
DELETE /users/:user_id/addresses/:id(.:format) client/addresses#destroy
If you are using AJAX to submit the form, try adding method: 'POST' to it.
If you are submitting the form normally, try adding method: :post to the form_for hash.
It should end up something like:
<%= form_for([#address.user, #address], url: user_addresses_path, method: :post) do |f| %>
I have two lines in my routes.rb that look like this:
resources :locations
get '/locations/:session_series_token/new', to: 'locations#new', as: "new_location"
I want the create method in my LocationsController, and I also want a specific locations#new path.
Removing the resources :locations line causes my #new form to error with:
undefined method `locations_path' for #<<Class:0x007fa18caeb3b8>:0x007fa18caea8f0>
Extracted source (around line #1):
<%= form_for [#location] do |form| %>
<h2>Class Location</h2>
There must be a way to do this correctly!
I think that since you are removing resources: locations there is no longer an "index" route for Location (called locations_path). Run the command rake routes and you will see that you are only left with a new_location_path.
Try specifying the path like so:
<%= form_for #location, url: new_location_path do |form| %>
Try this:
resources :locations, except: [:new]
get '/locations/:session_series_token/new', to: 'locations#new', as: "new_location"
As the resources :locations defines all the routes for you and you need to define only the new route explicitly so you can write like above. If you remove the resources :locations then your create route also gets removed and the line <%= form_for [#location] do |form| %> searches for the locations_path which is the create action.
Hope this helps.
i try to fill twice id in url, but when i send params twice id just one id fill the url id.
My route :
namespace :admin do
resources :stores
get "/:id/new_items"=> 'stores#new_items', as: :store_new_items
post "/:id/create_items"=> 'stores#create_items', as: :store_create_items
get "/:id/show_items/:id"=> 'stores#show_items', as: :store_show_items
get "/:id/items/:id/new_items_sub" => 'stores#new_items_sub', as: :store_new_items_sub
post "/:id/items/:id/create_items_sub" => 'stores#create_items_sub', as: :store_create_items_sub
get "/:id/items/:id/show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
end
my view :
<%= link_to "add new items", admin_store_new_items_sub_path(#store.id, #items.id), :class=> "btn" %>
i hope my url like this :
http://localhost:3000/admin/#{store.id}/items/#{items.id}/new_items_sub
but i get same id like this :
http://localhost:3000/admin/#{store.id}/items/#{store.id}/new_items_sub
please tell me when i'm wrong? thanks
you have to create neseted routes for that .have a look at
http://guides.rubyonrails.org/routing.html#nested-resources
for example
resources :publishers do
resources :magazines do
resources :photos
end
end
will accept routes /publishers/1/magazines/2/photos/3
Your params should be unique, so you can't pass more than one different :id params. Instead. you can do something like:
get '/:store_id/show_items/:id', as: :store_show_items
and in view:
<%= link_to 'show items', store_show_items_path(#store.id, #item.id) %>
Also, you should read more about Resources and Nested Resources in Rails, there's probably no need to complicate your life by creating each route independently.
You could refactor this to use nested routes like this (you may have to change controller method names):
namespace :admin do
resources :stores do
resources :items, :only => [:new, :create, :show] do
resources :subs, :only => [:new, :create, :show]
end
end
end
This would give you a few url helpers like this: new_store_item_sub_path(#store.id, #item.id) for the new action and store_item_sub_path(#store.id, #item.id, #sub.id) for the show action.
Run rake routes to see what helpers and routes you have access to.
Have a look here to find out more about nested routes.
Your code can be DRYed up significantly. Hopefully this works; might need some tweaking:
namespace :admin do
resources :stores do
member do
get :new_items, as: :store_new_items
post :create_items, as: :store_create_items
end
get "show_items/:id"=> 'stores#show_items', as: :store_show_items
resources :items do
get :new_items_stub => 'stores#new_items_sub', as: :store_new_items_sub
post :create_items_stub => 'stores#create_items_sub', as: :store_create_items_sub
get "show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
end
end
end
Uses Member Routes (see 2.10) & Nested Resources
Nested Resources
The crux of your issue is that you're trying to pass the :id param twice
Fortunately, Rails has a solution to this, in the form of Nested Resources. These work by taking the "parent" id and prepending a singular prefix, such as :store_id, allowing you to use the :id param for another set of methods
I'm trying to call a custom controller action shuffle for a resource that is nested within another resource. I can't seem to get the method call right.
routes.rb
resources :templates do
resources :items
end
match "/templates/:template_id/items/shuffle" => "items#shuffle"
I have a link in my items#index view:
<%= link_to 'Shuffle', shuffle_template_items_path(#template) %>
When I click on the link, I get the following error:
undefined method `shuffle_template_items_path' for #<#<Class:0x42577c8>:0x3e77578>
I have also tried <%= link_to 'Shuffle', template_items_shuffle_path(#template) %> and that did not work.
How do I correctly call this custom action?
You probably want this:
resources :templates do
resources :items do
get :shuffle, :on => :collection
end
end
If you want your custom action to have a name, you need to provide it:
match "/templates/:template_id/items/shuffle" => "items#shuffle", :as => :suffle_template_items
I think the best way to write shuffle is in collection as per the documentation of Rails Routes:
So it would looks like this:
resources :templates do
resources :items do
collection do
get :shuffle
end
end
end
when you try rake routes you will find shuffle_template_items GET /templates/:template_id/items/shuffle(.:format) items#shuffle.