[英]Rails nested routes for devise user
您好,我的路由出現問題。 我目前已經為設計用戶提供了一個嵌套的帖子資源。 目前,我正在嘗試為每個用戶創建一個新帖子。
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 /新的未定義方法`user_posts_path'中的#<#:0x007fe01d1608f0>出現NoMethodError。 有任何想法嗎? 謝謝!
它不能解決問題,但是new_users_post_path
應該沒有任何參數:
<%= button_to "New Post", new_users_post_path, class: "btn btn-default navbar-btn", method: :get %>
也resource :user
代替users
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.