我发现没有路由匹配 {:action=>"new", :controller=>"posts"} 即使它不在我的 paths.rb 文件中。帮助?
好的,这是我发布到 stackoverflow 的第一个问题。我对编码还很陌生,过去 3 个月一直在学习 Ruby on Rails。我的代码可能有一些问题,但就这样了。
基本上我正在尝试让用户发帖。我正在使用 Devise 进行注册,所有这些都有效。但是,当我在标题视图中创建指向“创建帖子”的链接时,它告诉我没有匹配的路线 =>尽管我认为它存在。我相信我错过了一些小东西,但在我为尝试使其正确而所做的所有调试中,我认为我可能在此过程中搞砸了其他东西。下面附上了我的routes.rb 代码、我的post_controller 文件和我的布局视图文件。抱歉,我的胡言乱语,我无法简明扼要。有人看出有什么问题吗?如果您需要查看其他代码,请告诉我
_header.html.erb
<% if user_signed_in? %>
Signed in as <%= current_user.username %>. Not you?
<%= link_to "Logout", destroy_user_session_path, :method => :delete, %>
<%= link_to "Create Post", new_user_post_path %>
<%= link_to "Search", posts_index_path %>
<%= link_to "Show All", posts_show_path %>
paths.rb
#devise_for :users
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
match '/users/:user_id/posts/new', :to => 'posts#new'
resources :users do
resources :posts, :only => [:new, :create, :show, :index, :destroy]
end
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/safety', :to => 'pages#safety'
match '/privacy', :to => 'pages#privacy'
get 'posts/index'
get 'posts/show'
get 'posts/post'
match 'posts/search', :to => 'posts#search'
root :to => 'pages#home'
posts_controller
def create
if signed_in?
@user = current_user.posts.build(params[:user][:post])
if @user.save
flash[:success] = "Thanks for creating your post! " +
redirect_to new_user_post_path(@post)
else
render 'new'
def new
@title = "Create Post"
@post = Post.new
end
rake paths
users_sign_out GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
/users/:user_id/posts/new(.:format) {:controller=>"posts", :action=>"new"}
user_posts GET /users/:user_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /users/:user_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_user_post GET /users/:user_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
user_post GET /users/:user_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
DELETE /users/:user_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
safety /safety(.:format) {:controller=>"pages", :action=>"safety"}
privacy /privacy(.:format) {:controller=>"pages", :action=>"privacy"}
posts_index GET /posts/index(.:format) {:controller=>"posts", :action=>"index"}
posts_show GET /posts/show(.:format) {:controller=>"posts", :action=>"show"}
posts_post GET /posts/post(.:format) {:controller=>"posts", :action=>"post"}
posts_search /posts/search(.:format) {:controller=>"posts", :action=>"search"}
root /(.:format) {:controller=>"pages", :action=>"home"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在控制台中输入
rake paths
并检查您要查找的路由是否存在。请注意,顺序也很重要。Try typing
rake routes
in the console and check if the route you are looking for exists. Note that the order also matters.本来应该是
It should have been
我认为这是因为争论!
在您的路由文件中,您似乎还传递了 :user_id 。
在您的链接中尝试传递该 ID。
希望它有帮助...
<%= link_to "create Post", new_user_post_path(current_user.id) %>
另外我建议尝试重命名路径并使用它。
我知道这不是正确的方式,但仍然如此。
我遇到过类似的问题并通过这样做解决了!;)
希望它有帮助!
I think its because of the argument !
In your routes file it seems you are passing :user_id also.
In your link try passing that id.
Hope it helps...
<%= link_to "create Post", new_user_post_path(current_user.id) %>
Also I would suggest try renaming the path and use it.
I know its not proper way but still.
I had faced a similar problem and solved by doing this !!;)
Hope it helps !