I’m stuck and i don’t know why it is not working right. I have a model
lesson wich has many sublessons. When i update the lesson rails update
properly the lesson attributes but is creating another sublessons
records instead of just updating it.
Here is my code
View Form:
<%= sublesson_a.label :sublesson_name, "Subtitle 1" %></br>
<%= sublesson_a.text_field :sublesson_name, class:
“form-control” %>
<%= sublesson_a.label :sublesson_content, “Content” %>
<%=sublesson_a.text_area ‘sublesson_content’, rows: 3, class:
‘form-control’%>
<%= sublesson_a.label :sublesson_video_link, “Video link”
%>
<%= sublesson_a.text_field :sublesson_video_link, class:
“form-control” %>
<%end%>
<%= f.submit “Submit”, class: “btn btn-primary” %>
<% end %>
lesson model
class Lesson < ActiveRecord::Base
has_many :sublessons, :dependent => :destroy
accepts_nested_attributes_for :sublessons, :reject_if => :all_blank
end
sublesson model
class Sublesson < ActiveRecord::Base
belongs_to :lesson, :foreign_key => “lesson_id”
validates :sublesson_name, presence: true
validates :sublesson_content ,presence: true
end
lesson controller
def update
@lesson = Lesson.find(params[:id])
@[email protected]
if @lesson.update(lesson_params)
flash[:notice] = “Lesson updated”
redirect_to lessons_show_url
else
flash[:notice] = “Error”
redirect_to lessons_show_url
end
end
def edit
@lesson = Lesson.find(params[:id])
@[email protected]
end
def lesson_params
params.require(:lesson).permit(:lesson_name, :lesson_icon,
sublessons_attributes:[:sublesson_id, :sublesson_name,
:sublesson_content, :sublesson_video_link])
end