Ruby on rails ROR中的路由错误
我的索引页上有两个按钮Ruby on rails ROR中的路由错误,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我的索引页上有两个按钮 登记册 登录 如果单击,我应该重定向到new.html.erb页面,但当我单击“注册”按钮时,我发现以下错误。请帮我解决这个错误 错误: No route matches [POST] "/posts/new" 下面给出了我的代码片段 在posts/index.html.erb中 <h1>Index page</h1> <h1>Register Here</h1> <hr /> <p> <%=
new.html.erb
页面,但当我单击“注册”按钮时,我发现以下错误。请帮我解决这个错误
错误:
No route matches [POST] "/posts/new"
下面给出了我的代码片段
在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>
<h1>Register Here</h1>
class PostsController < ApplicationController
def index
end
def new
@user=User.new
end
end
Rails.application.routes.draw do
root "posts#index"
get "posts/new" => "posts#new"
#resources:posts
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
我使用的是rails版本4
posts\u new\u应用程序中没有定义路径
我建议您查看以获得更深入的理解,但解决此问题的最快方法是将routes.rb文件更改为:
Rails.application.routes.draw do
root "posts#index"
get "posts/new" => "posts#new", as: new_post
#resources:posts
end
在你看来:
<%= button_to "REGISTER",new_post_path%>
在routes中使用as
方法,使用末尾用path
或url
指定的短语创建命名路由,然后可以在视图中使用
您可以使用短语
resources:posts
,这将在应用程序中为7个CRUD操作公开许多命名路由。请阅读指南中的相关内容,因为这可能对您有好处,具体取决于您的应用程序的结构。posts\u new\u路径在您的应用程序中没有定义
我建议您查看以获得更深入的理解,但解决此问题的最快方法是将routes.rb文件更改为:
Rails.application.routes.draw do
root "posts#index"
get "posts/new" => "posts#new", as: new_post
#resources:posts
end
在你看来:
<%= button_to "REGISTER",new_post_path%>
在routes中使用as
方法,使用末尾用path
或url
指定的短语创建命名路由,然后可以在视图中使用
您可以使用短语resources:posts
,这将在应用程序中为7个CRUD操作公开许多命名路由。请阅读本指南,因为这可能对您有好处,具体取决于您的应用程序的结构。按钮的默认方法是:post
,您需要使用方法get选项调用它:
<%= button_to "REGISTER", posts_new_path, method: :get %>
请参阅按钮的默认方法是:post
,您需要使用方法get选项调用它:
<%= button_to "REGISTER", posts_new_path, method: :get %>
请参阅检查rake路由的o/p,您将找到新帖子可用的生成路由。按钮_生成一个表单,用于提交带有post请求的数据。检查视图和路由中的方法类型检查rake路由的o/p,您将找到新帖子的可用生成路由。按钮_生成一个表单,用于提交带有post请求的数据。检查视图和管线中的方法类型