2011-08-28 28 views
2

好吧,这是我第一个发布到stackoverflow的问题。我对编码相当陌生,过去3个月一直在学习Ruby on Rails。我的代码可能存在一些问题,但是在这里。我是gettgin没有路由匹配{:action =>“new”,:controller =>“posts”},即使它不在我的routes.rb文件中。帮帮我?

基本上我试图让用户发布。我正在使用Devise进行注册,所有这些工作。但是当我在Header视图中创建一个“Create Post”的链接时,它告诉我我没有匹配=>的路由,即使我认为它存在。我错过了一些我相信的东西,但在所有调试过程中,我尝试着去做正确的事情,我想我可能在这一路上干扰了其他事情。下面是我的代码为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 

耙路线

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

我忘了加我耙路线路径 – abhi

回答

1

尝试在控制台中键入rake routes并检查您正在查找的路线是否存在。请注意,订单也很重要。

+0

是它存在,它的访问我的职位,新的 – abhi

+0

控制器new_user_post GET /users/:user_id/posts/new(.:format的唯一路径){:动作=> “new”,:controller =>“posts”} – abhi

+0

我的意思是,你可以粘贴'rake routes'的输出吗?那么看看new_user_posts是否真的存在 –

1

应该已经

<%= link_to "Create Post", new_user_post_path(current_user) %> 
+0

啊哟谢谢@dexter。但现在它说我在我的“新”变量上得到一个例外...'code'def new '@'title =“Create Post” '@'post = Post.new end – abhi

+0

now now updated in my post_controller代码现在:) – abhi

0

我认为它的说法,因为! 在你的路线文件,它似乎你传递:user_id也。 在你的链接尝试传递该ID。 希望它可以帮助...

<%=的link_to “创建邮报”,new_user_post_path(current_user.id)%>

此外,我建议尝试重命名路径,并使用它。 我知道它不正确的方式,但仍然。 我曾遇到类似的问题,并通过这样做解决!!;)

希望它有帮助!

+0

嗯,我现在看到正确的url路径,但我得到这个消息:Users/abhisharma/rails_projects/projectx/app/controllers/posts_controller.rb:21:语法错误,意外的tINTEGER ,期待';'或'\ n' ... 1111111111111111111111111111111qqqqqqqqqqqqqqqqqqqqqqqqqqqqq ... 为什么会出现?我以前没有见过。再次感谢您的帮助! – abhi

+0

这是指我的def新代码,我更新了我的代码。 – abhi

相关问题