Rails嵌套路由为设计用户

时间:2014-09-02 15:18:28

标签: ruby-on-rails devise

您好我的路由问题。我目前使用嵌套的post资源设计用户。目前我正在努力为每个用户创建一个新帖子。

的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

这会导致: / users / posts / new中的NoMethodError 未定义的方法`user_posts_path&#39;对于#&lt;#:0x007fe01d1608f0&gt;。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

它无法解决问题,但new_users_post_path应该没有任何参数:

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

resource :user代替users