2014-09-02 99 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