2014-09-02 104 views
0

你好,我的路由问题。我目前有用嵌套后期资源设计用户。目前我正在尝试为每个用户创建一个新帖子。设计用户的Rails嵌套路由

的routes.rb

Rails.application.routes.draw do 

    root 'home#index' 

    resources :dashboard 

    devise_for :users, :path => '', :path_names => { 
    :sign_in => 'login', 
    :sign_out => 'logout', 
    :sign_up => 'register' 
    } 

    resource :users do 
    resources :posts 
    end 
end 

posts_controller.rb

class PostsController < ApplicationController 
    before_action :set_user 

    def index 
    @posts = @user.posts 
    end 

    def new 
    @post = @user.posts.new 
    end 

    def create 
    @post = @user.posts.new(post_params) 
    if @post.save 
     redirect_to :controller => 'dashboard', :action => 'index' 
    else 
     render :new 
    end 
    end 

private 

    def post_params 

    end 

    def set_user 
    @user = current_user 
    end 
end 

链接

<%= button_to "New Post", new_users_post_path(current_user), :class => "btn btn-default navbar-btn", :method => :get %> 

路线

new_users_post GET /users/posts/new(.:format)  posts#new 

这导致: NoMethodError在/用户/职位/新 未定义的方法`user_posts_path”为#<#:0x007fe01d1608f0>。有任何想法吗?谢谢!

回答

0

它不能解决问题,但new_users_post_path应该没有任何PARAMS:

<%= button_to "New Post", new_users_post_path, class: "btn btn-default navbar-btn", method: :get %> 

resource :user代替users

+0

谢谢!但我仍然遇到同样的错误。 (未定义的方法'user_posts_path'为#<#:0x007fe01d0339f0>) – vertroa 2014-09-02 15:51:37