如果在不同的模型上满足条件,我试图在表单中显示某些字段。
以下是示例:
<%= form_for(@workout) do |f| %>
<%= f.fields_for :workout_exercises do |s| %>
<%= s.collection_select :exercise_id, Exercise.all, :id, :name, { :include_blank => ""} %>
<%= s.label :sets %>:
<%= s.number_field :set %>
<% if @exercise.is_cardio == true %>
<%= s.label :time %>(Minutes):
<%= s.number_field :time %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
上面给出is_cardio
上没有方法错误有氧运动是练习表中的布尔字段
谢谢!
编辑:
class Exercise < ActiveRecord::Base
has_many :workout_exercises
has_many :workouts, :through => :workout_exercises
end
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, :through => :workout_exercises
class WorkoutExercise < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
end
编辑2:
具体来说,这是我得到的错误:
undefined method `is_cardio' for #<Array:0x007fcd3bb93dd0>
答案 0 :(得分:1)
我的猜测是你需要改变
<% if @exercise.is_cardio == true %>
<%= s.label :time %>(Minutes):
<%= s.number_field :time %>
<% end %>
到
<% if s.object.exercise.is_cardio == true %>
<%= s.label :time %>(Minutes):
<%= s.number_field :time %>
<% end %>