【问题标题】:Rails-Simple Form and Devise current_user issue when using model associations使用模型关联时 Rails-Simple Form 和 Devise current_user 问题
【发布时间】:2016-05-31 06:19:25
【问题描述】:

我使用 Devise 作为我的身份验证系统和简单的表单。我在 Groups#show 中收到 NoMethodError 和 undefined method 'name' for nil:NilClass 错误。我使用模型关联将组和帖子联系在一起。当我执行puts post.user.name 时,它会正确显示在我的终端中,但该行会导致上述错误,并且出于某种原因它正在引用 Groups#show。有什么想法吗?

路线

resources :groups do
    resources :posts
end

组模型

class Group < ActiveRecord::Base
  validates :user_id, presence: true  

  belongs_to :user
  has_many :posts
  has_many :comments
  has_many :attachments
end

后模型

class Post < ActiveRecord::Base
  validates :user_id, presence: true  
  validates :caption, presence: true

  belongs_to :user
  belongs_to :group

  has_many :comments, dependent: :destroy
end

组控制器

class GroupsController < ApplicationController
before_action :authenticate_user!

def new
    @group = current_user.groups.build
end

def create
    @group = current_user.groups.build(group_params)
    @group.user_id = current_user.id
    if @group.save
        redirect_to groups_path
    else
        render :new
    end
end

...
private

def group_params
    params.require(:group).permit(:group_name, :description, :user_id)
end

end

帖子控制器

class PostsController < ApplicationController

  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :owned_post, only: [:edit, :update, :destroy]  
  before_action :authenticate_user!

def index
    @posts = Post.paginate(page: params[:page], per_page: 3).order('created_at DESC')
    @post = current_user.posts.build
    @attachments = Attachment.all
end

...

def new
    @post = current_user.posts.build
end

def create
    @post = current_user.posts.build(post_params)
    @group = Group.find(params[:group_id])
    @post.group_id = @group.id

    if @post.save
      redirect_to groups_path
    else
      render :new
    end
end
...
private

def post_params
    params.require(:post).permit(:caption, :user_id)
end

def set_post
  @post = Post.find(params[:id])
end

def owned_post  
  unless current_user == @post.user
  redirect_to root_path
end

end  

end

groups/show.html.erb

<%= render "posts/index" %> 
...

posts/_index.html.erb

<%= render 'posts/form' %>
<%= render 'posts/posts' %>
...

posts/_form.html.erb

<%= simple_form_for([@group, @group.posts.build]) do |f| %>
...

posts/_posts.html.erb

<% @group.posts.each do |post| %>
<%= puts post.user.name %> ISSUE
<%#<%= render 'posts/post', post: post %>
<% end %>

【问题讨论】:

  • 在某些帖子中似乎没有与之关联的用户
  • 我有 作为测试,所以当它是 user_id 时,它会在终端中输出正确的 user_id 但是当我做 user_name 时它不能正常工作。当我在我的帖子创建方法中执行 @post = current_user.posts.build(post_params) 并且没有正确链接组和帖子时,这可能是一个问题吗?
  • 您手动删除用户了吗?试试这个 post.try(:user).try(:name)
  • &lt;%= puts post.try(:user).try(:name) %&gt; 在终端中输出正确的用户名。但是,由于某种原因,当我尝试在我的视图中渲染它时,它会崩溃并且我收到上面提到的错误。我不确定为什么会发生这种情况。
  • 那么错误与帖子无关,可能与其他一些声明有关

标签: ruby-on-rails devise simple-form


【解决方案1】:

解决方案:在 Reddit Rails 上询问后,一位慷慨的用户提供了一个可行的解决方案。显然&lt;%= simple_form_for([@group, @group.posts.build]) do |f| %&gt; 创建了一个新帖子并将其添加到 groups.posts 数组中,因此当它迭代 _posts.html.erb 并且没有用户时会导致问题。更多信息可以在这里找到:https://www.reddit.com/r/rails/comments/4lygix/unidentified_method_for_nil_class_devise_and/

但是用&lt;%= simple_form_for([@group, Post.new(group: @group)]) do |f| %&gt; 替换上面的simple_form 代码行似乎可以解决问题,正如上面reddit 链接中慷慨的用户所建议的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多