简体   繁体   English

Rails为设计用户嵌套了路线

[英]Rails nested routes for devise user

Hello I have an issue with my routing. 您好,我的路由出现问题。 I currently have devise user with a nested post resource. 我目前已经为设计用户提供了一个嵌套的帖子资源。 Currently I am trying to be able to create a new post for each user. 目前,我正在尝试为每个用户创建一个新帖子。

routes.rb 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 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

link 链接

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

route 路线

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

This leads to: NoMethodError at /users/posts/new undefined method `user_posts_path' for #<#:0x007fe01d1608f0>. 这导致:/ users / posts /新的未定义方法`user_posts_path'中的#<#:0x007fe01d1608f0>出现NoMethodError。 Any ideas? 有任何想法吗? Thanks! 谢谢!

It doesn't resolve problem, but new_users_post_path should be without any params: 它不能解决问题,但是new_users_post_path应该没有任何参数:

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

also resource :user instead users resource :user代替users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM