ruby on rails - Routing Error in ROR -
i have 2 button in index page
- register
- login
if clicked should redirect new.html.erb
page, when clicked in register button found below error. please me resolve error.
error:
no route matches [post] "/posts/new"
my code snippets given below.
in posts/index.html.erb
<h1>index page</h1> <h1>register here</h1> <hr /> <p> <%= button_to "register",posts_new_path%> </p> <p> <%= button_to "login" %> </p>
in posts/new.html.erb
<h1>register here</h1>
in controller/posts_controller.rb
class postscontroller < applicationcontroller def index end def new @user=user.new end end
in config/routes.rb
rails.application.routes.draw root "posts#index" "posts/new" => "posts#new" #resources:posts # priority based upon order of creation: first created -> highest priority. # see how routes lay out "rake routes". # can have root of site routed "root" # root 'welcome#index' # example of regular route: # 'products/:id' => 'catalog#view' # example of named route can invoked purchase_url(id: product.id) # 'products/:id/purchase' => 'catalog#purchase', as: :purchase # example resource route (maps http verbs controller actions automatically): # resources :products # example resource route options: # resources :products # member # 'short' # post 'toggle' # end # # collection # 'sold' # end # end # example resource route sub-resources: # resources :products # resources :comments, :sales # resource :seller # end # example resource route more complex sub-resources: # resources :products # resources :comments # resources :sales # 'recent', on: :collection # end # end # example resource route concerns: # concern :toggleable # post 'toggle' # end # resources :posts, concerns: :toggleable # resources :photos, concerns: :toggleable # example resource route within namespace: # namespace :admin # # directs /admin/products/* admin::productscontroller # # (app/controllers/admin/products_controller.rb) # resources :products # end end
i using rails version-4
posts_new_path
isn't defined anywhere in application.
i'd suggest take @ rails routing guide gain deeper understanding quickest way fix change routes.rb file read:
rails.application.routes.draw root "posts#index" "posts/new" => "posts#new", as: new_post #resources:posts end
and in view:
<%= button_to "register",new_post_path%>
using as
method in routes creates named route using phrase specified either path
or url
on end, can used in views.
you use phrase resources :posts
expose number of named routes in application 7 crud actions. read in guide on may depending on how application structured.
Comments
Post a Comment