Rails 嵌套表单 - 按当前用户、课程-问题-答案-用户过滤

发布于 2024-09-28 19:24:12 字数 1739 浏览 4 评论 0原文

我有一个基于以下模型的嵌套表单——一堂课有很多问题,每个问题有很多答案,答案属于用户。

我正在开发一个嵌套表单,以便新用户可以查看问题并发布答案。如果用户过去输入过答案,我希望这些答案显示出来;否则显示空白字段。我也不希望用户看到其他人的答案。

所以,我不知道如何只显示当前登录用户的答案。我创建了一个named_scope,但它不起作用(请参阅我的编辑操作)。现在,在编辑时,我可以在每个问题下看到所有用户的答案。 为了构建视图,我遵循了 Railscast 196 中的嵌套表单示例。

感谢您的帮助。 这是显示我的模型和课程控制器的代码。

    class Lesson < ActiveRecord::Base
      has_many :questions, :dependent => :destroy
      accepts_nested_attributes_for :questions, :allow_destroy => true, 
:reject_if => proc { |a| a['data'].blank? }
    end

    class Question < ActiveRecord::Base
      belongs_to :lesson
      has_many :answers
      accepts_nested_attributes_for :answers, 
:reject_if => lambda { |a| a['data'].blank? }, :allow_destroy => true
    end

    class Answer < ActiveRecord::Base
      belongs_to :question
      belongs_to :user
      named_scope :by_user, 
lambda {|user| {:conditions => ["user_id = ?", user]}}
    end

    class User < ActiveRecord::Base
      has_many :answers 
      accepts_nested_attributes_for :answers, 
:reject_if => lambda { |a| a['name'].blank? }, :allow_destroy => true
    end

LESSONS Controller:



def edit
    @lesson = Lesson.find(params[:id])
    if current_user_admin == 99 # show blank question field if admin user
      @questions = @lesson.questions.build(:user_id => current_user)
    end
    @lesson.questions.each do |question|
      # if there are no answers for this user 
      if question.answers.by_user(current_user.id).size != 1
        # if the current user is not admin
        if current_user_admin != 99
          question.answers.by_user(current_user.id).build(:user => current_user)
        end 
      end 
    end 
  end

I have a nested form that is based on the following model -- A lesson has many questions, each question has many answers, and answers belong to users.

I am developing a nested form, so that a new user can review the questions and post answers. If a user has entered answers in the past, I want those to show up; otherwise show blank fields. I also don't want the user to see anyone else's answers.

So, I can't figure out how to show only the answers for the currently logged-in user. I created a named_scope, but it doesn't work(see my edit action). Right now, when editing, I see the answers for all users underneath each question.
To build the view I followed the nested form example from Railscast 196.

Thank you for your help.
Here is the code showing my models and the lessons controller.

    class Lesson < ActiveRecord::Base
      has_many :questions, :dependent => :destroy
      accepts_nested_attributes_for :questions, :allow_destroy => true, 
:reject_if => proc { |a| a['data'].blank? }
    end

    class Question < ActiveRecord::Base
      belongs_to :lesson
      has_many :answers
      accepts_nested_attributes_for :answers, 
:reject_if => lambda { |a| a['data'].blank? }, :allow_destroy => true
    end

    class Answer < ActiveRecord::Base
      belongs_to :question
      belongs_to :user
      named_scope :by_user, 
lambda {|user| {:conditions => ["user_id = ?", user]}}
    end

    class User < ActiveRecord::Base
      has_many :answers 
      accepts_nested_attributes_for :answers, 
:reject_if => lambda { |a| a['name'].blank? }, :allow_destroy => true
    end

LESSONS Controller:



def edit
    @lesson = Lesson.find(params[:id])
    if current_user_admin == 99 # show blank question field if admin user
      @questions = @lesson.questions.build(:user_id => current_user)
    end
    @lesson.questions.each do |question|
      # if there are no answers for this user 
      if question.answers.by_user(current_user.id).size != 1
        # if the current user is not admin
        if current_user_admin != 99
          question.answers.by_user(current_user.id).build(:user => current_user)
        end 
      end 
    end 
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-10-05 19:24:12

该命名范围看起来应该对我有用。您确定数据库中的答复记录的 user_id 设置正确吗?

我认为您在 reject_if lambda 中获得的哈希值的键是字符串而不是符号,因此您的嵌套模型字段可能会被默默地拒绝。

That named scope looks like it should work to me. Are you sure that the answer records in your database have a user_id set properly?

I think that the hash you get in the reject_if lambda has keys that are strings rather than symbols so your nested models fields may be silently getting rejected.

橘和柠 2024-10-05 19:24:12

Iv 发现控制器中的代码存在问题。您正在每个块内构建一个答案对象,该对象迭代答案,只有当答案为零时,这永远不会发生。

我认为你在控制器中尝试做的事情是这样的:

def edit
  @lesson = Lesson.find(params[:id])
  @lesson.questions.each do |question|
    if question.answers.by_user(current_user.id).empty?
      question.answers.build(:user => current_user)
    end
  end
end

Iv spotted a problem with the code in your controller. You are building an answer object inside the each block which iterates through answers, only if that answer is nil, which will never happen.

I think your what you are trying to do in your controller is something like:

def edit
  @lesson = Lesson.find(params[:id])
  @lesson.questions.each do |question|
    if question.answers.by_user(current_user.id).empty?
      question.answers.build(:user => current_user)
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文