簡體   English   中英

Rails為設計用戶嵌套了路線

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM