好的,这是我发布到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 %>
的routes.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 routes
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"}
答案 0 :(得分:1)
尝试在控制台中输入rake routes
,然后检查您要查找的路线是否存在。请注意,订单也很重要。
答案 1 :(得分:1)
应该是
<%= link_to "Create Post", new_user_post_path(current_user) %>
答案 2 :(得分:0)
我认为这是因为这个论点! 在您的路由文件中,您似乎正在传递:user_id。 在你的链接尝试传递该ID。 希望它有所帮助...
&lt;%= link_to“create Post”,new_user_post_path(current_user.id)%&gt;
另外我建议尝试重命名路径并使用它。 我知道它不是正确的方法,但仍然。 我遇到了类似的问题并通过这样做解决了!!;)
希望它有所帮助!