简体   繁体   English

使用collection_select进行validates_uniqueness_of时出现Ruby on Rails错误

[英]Ruby on Rails error when validates_uniqueness_of using collection_select

First, sorry for my bad English. 首先,对不起我的英语不好。 I'm still learning. 我还在学习。

I have 3 tables in my DB: 我的数据库中有3个表:

Problem 问题

  • has_many :registers
  • has_many :solutions, through : :registers

Solution

  • has_many :problems
  • has_many :problems, through : :registers

Register 寄存器

  • belongs_to: problem
  • belongs_to :solution

The system is working well. 系统运行良好。 I am able to insert new data in all of the 3 tables. 我能够在所有3个表中插入新数据。

In the views for the table/model Register , to select problems and solutions, I make use of collection_select, like this: 在表/模型Register的视图中,要选择问题和解决方案,我使用collection_select,如下所示:

   = form_for @register do |f|

  .field
    = f.label :problem_id
    = collection_select( :register, :problem_id, @problems, :id, :name, {}, { :multiple => false })
  .field
    = f.label :solution_id
    = collection_select( :register, :solution_id, @courses, :id, :name, {}, { :multiple => false })
  .field
    = f.label :entry_at
    = f.datetime_select :entry_at
  .actions = f.submit

The problem only appears when I try to add this validation to Register : 仅当我尝试将此验证添加到Register时,才会出现问题:

validates_uniqueness_of :student_id , scope: :course_id

Then I get: 然后我得到:

> undefined method `map' for nil:NilClass 
> = collection_select( :register, :problem_id, @problems, :id, :name, {}, { :multiple => false })

And I dont know why. 而且我不知道为什么。

So, I tried to do the validation by the controller: 因此,我尝试通过控制器进行验证:

def create
  @register = Register.new(register_params)
  problem_id = @register.problem_id 
  solution_id = @register.solution_id
  if Register.exists?(['problem_id LIKE ? AND solution_id LIKE ?', problem_id, solution_id ])
    render 'new'
  else
    @register.save
    respond_with(@register)
  end
end

But the error remains. 但是错误仍然存​​在。

I believe that the cause is the collection_select, but I don't know how to solve it. 我相信原因是collection_select,但我不知道如何解决。

Saying one more time, I am able to persist date in all the 3 DB tables. 再说一次,我可以将日期保留在所有3个DB表中。 But when I try to avoid duplication, the error appears. 但是,当我尝试避免重复时,会出现错误。

This is how I solve this problem: 这就是我解决这个问题的方法:

def create
        @register = register.new(register_params)

        #if @register.save
        #  respond_with(@register)
        #else
        #  @register = register.all
        #  render :new
        #end
        problem_id = @register.problem_id 
        solution_id = @register.solution_id

        if register.exists?(['problem_id LIKE ? AND solution_id LIKE ?', problem_id, solution_id ])
          @register = register.new
          @solutions = Solution.all
          @problems = Problem.all
          flash[:error] = "Problem alread in the register for this solution"
          render 'new'
        else
          @register.save
          respond_with(@register)
        end 

      end

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

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