简体   繁体   中英

Error with simple_form on Rails

i'm trying to render a form but it's throwing an error and I can't find a reason for it.

controller

def new
 @student = Student.find(params[:student_id])
 @learning_instrument =  LearningInstrument.new
end

def create

 @student = Student.find(params[:student_id])
 @learning_instrument = LearningInstrument.new(learning_instrument_params)

 @learning_instrument.student = @student


 if @learning_instrument.save
   redirect_to student_path(@student)
 else
   render :new
 end
 end


private

 def learning_instrument_params
  params.require(:learning_instrument).permit(:level, :student_id, :instrument_id)
 end

this is my form

 <%= simple_form_for(@student, @learning_instrument) do |f| %>
   <%= f.error_notification %>

   <%= f.input :level %>
   <%= f.association :instrument, collection: Instrument.all %>
   <%= f.hidden_field :student, value: @student %>

   <%= f.button :submit %>
<% end %>

I'm getting "can't write unknown attribute builder ". I've done stuff like this some times and it worked but this time I can't get it to work

Thanks

can't write unknown attribute builder

This is because of this line <%= simple_form_for(@student, @learning_instrument) do |f| %> <%= simple_form_for(@student, @learning_instrument) do |f| %> .You have to define simple_form_for for nested resources like below

<%= simple_form_for [@student, @learning_instrument] do |f| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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