Answer the question
In order to leave comments, you need to log in
What could be the causes of NoMethodError when validating a collection_select field that refers to another model?
There is a SubClass model with field validation:
belongs_to :func_class
validates :func_class_id, presence: true
validates :title, presence: { message: "Заголовок не должен быть пустым" }
validates :title, length: { in: 3..500, message: "Длина Заголовка должна быть от 3 до 500 символов" }
def new
@sub_class = SubClass.new
@func_class_all = FuncClass.all
end
<%= form_for(@fire_sub_class) do |f| %>
.....
<div class="field">
Title:<br>
<%= f.text_field :title %>
</div>
<div class="field">
Func class:<br>
<%= f.collection_select(:func_class_id, @func_class_all, :id, :title) %>
</div>
.....
has_many :sub_class, dependent: :destroy
validates :title, presence: { message: "Заголовок не должен быть пустым" }
validates :title, length: { in: 3..254, message: "Длина Заголовка должна быть от 3 до 254 символов" }
validates :title, uniqueness: { message: "Заголовок должен быть уникальным!" }
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"iXBbWvvPlsIYqby0lKEJYTsUAaMl1T54tKFumDmRAWE=",
"sub_class"=>{"title"=>"",
"func_class_id"=>"1"},
"commit"=>"OK"}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question