NoMethodError(nil:NilClass的未定义方法“ posts”):

时间:2019-09-12 05:08:11

标签: ruby-on-rails ruby devise

有人请帮我,我发送了本地服务器的post方法,但是发生了上述错误,所以我尝试了很多事情来重新创建表,更改路径和模型名称等等。 但是我无法解决它,我想知道如何修复它以及为什么会出现此错误,

  

Ruby 2.5.3 Rails 6.0.0 rc2

控制器

class PostsController < ApplicationController
  before_action :authenticate_user!, only: [:new]

  def index
    @posts = current_user.posts.all
  end

  def new
    @post = Post.new
  end

# Maybe here is happening error

  def create
    if @post = current_user.posts.build(posts_params)
      flash[:success] = "You created post"
      redirect_to @posts
    else
      flash[:failed] = "You failed  posted "
    end
  end

  def edit
  end

  def show
  end

private

  def posts_params
    params.require(:posts).permit(:title , :content , :user_id)
  end
end

路线

Rails.application.routes.draw do

  devise_for :users , :paths => 'users'

  resources :users do
    resources :posts , :except => :edit
  end
  root 'users#index'
end

用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :authentication_keys => [:name]

  validates :name, presence: true, uniqueness: true , length:{maximum: 10}

  has_many :posts

谢谢大家,问题似乎是current_user

因此,我使用byebug,以便(byebug)current_user   用户负载(0.5毫秒)选择users。*从usersusersid = 1 ORDER BY usersid ASC LIMIT 1   ↳(byebug):1:在“创建”中

再次感谢大家,我重新编写了代码

class PostsController < ApplicationController

 before_action :authenticate_user!, only: [:index, :new, :create]



 def index
   @posts = current_user.posts.all
 end

 def new
  @post = Post.new
 end

 def create

   if @post = current_user.posts.create(posts_params)
   flash[:success] = "You created post"
    redirect_to user_posts_path
  else
    flash[:failed] = "You failed  posted "
  end
end

 def edit
 end

def show
end

private
  def posts_params
    params.require(:posts).permit(:title , :content , :user_id)
  end
end

不幸的是,此代码也无法正常工作,以防万一,我编写了表单代码

<%= form_with model: @post , url: user_posts_path  ,local: true do |f| %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :content %>
<%= f.text_area :content %>

<% f.label :user_id %>
<% f.hidden_field :user_id %>

<%= f.submit  "Create Post" %>
   

3 个答案:

答案 0 :(得分:0)

在控制器中,您只需要用户登录即可执行操作new。这导致其他所有页面用户都可以在未经身份验证的情况下访问/交互。

因此,如果没有登录用户使用此呼叫undefined posts for nil

,则使用您的控制器代码,您将得到错误current_user.posts
class PostsController < ApplicationController
  before_action :authenticate_user!, only: [:new]

  def index
    @posts = current_user.posts.all
  end

  def new
  end

  def create
    if @post = current_user.posts.build(posts_params)
     # ...
    end
  end

  def edit
  end

  def show
  end

end

要解决此错误,您可以添加更多需要用户身份验证的操作

class PostsController < ApplicationController
  before_action :authenticate_user!, only: [:index, :new, :create]
end

或仅要求控制器的所有操作

class PostsController < ApplicationController
  before_action :authenticate_user!
end

答案 1 :(得分:0)

代码中有几个错误:

  1. 检查用户的创建操作

    before_action :authenticate_user!, only: %i[new create]
    
  2. 使用create方法代替build方法并解决重定向问题。

    def create
      if @post = current_user.posts.create(posts_params)
        flash[:success] = "You created post"
        redirect_to @post
      else
        flash[:failed] = "You failed  posted"
      end
    end
    
  3. 但是,如果遇到错误,请检查current_user的设计配置。

答案 2 :(得分:0)

更改
class PostsController < ApplicationController
  before_action :authenticate_user!, only: [:new]
end

class PostsController < ApplicationController
  before_action :authenticate_user!
end

因为您在current_user操作中调用了index,基本上我认为所有操作都需要用户登录。