Hello! I have a little problem saving some data in database... I have to models..... class Lesson < ActiveRecord::Base has_many :sublessons, :dependent => :destroy accepts_nested_attributes_for :sublessons end
and
class Sublesson < ActiveRecord::Base belongs_to :lesson validates :subblesson_name, presence: true validates :sublesson_content ,presence: true end
I generated a form for add another sublesson like this (i know isn't best way but i don't find another way)
<div style="padding:13px"> <%= form_for @lesson do |f| %> <%= f.label :lesson_name, "Lesson title" %></br> <%= f.text_field :lesson_name, class: "form-control" %></br> <%= f.label :lesson_icon, "Choise icon" %> <%= f.select "lesson_icon", options_for_select([ "ico03", "ico04","ico05","ico06" ])%></br></br>
<div> <%= f.fields_for :sublesson do |sublesson_a| %> <%= sublesson_a.label :sublesson_name, "Subtitle 1" %></br> <%= sublesson_a.text_field :sublesson_name, class: "form-control" %></br> <%= sublesson_a.label :sublesson_content, "Content" %></br> <%=sublesson_a.text_area 'sublesson_content', rows: 3, class: 'form-control'%></br> <%= sublesson_a.label :sublesson_video_link, "Video link" %><br> <%= sublesson_a.text_field :sublesson_video_link, class: "form-control" %></br> </div>
<%=(1..50).each do |i|%> <div class="add_new_subtitle"> <%= sublesson_a.label :sublesson_name, "Subtitle 1" %></br> <%= sublesson_a.text_field :sublesson_name, class: "form-control" %></br> <%= sublesson_a.label :sublesson_content, "Content" %></br> <%=sublesson_a.text_area 'sublesson_content', rows: 3, class: 'form-control'%></br> <%= sublesson_a.label :sublesson_video_link, "Video link" %><br> <%= sublesson_a.text_field :sublesson_video_link, class: "form-control" %></br> </div> <%end%>
<%end%> <a href="javascript:;" style="float:right" class="btn btn-primary" id="addNewTag">Add Sublesson</a> <%= f.submit "Submit", class: "btn btn-primary" %> <% end %>
</div>
This is my methods def create #render text: params.inspect @lesson = Lesson.new(lesson_params) if @lesson.save flash[:notice] = "You have successfully add a lesson." redirect_to lessons_new_lesson_url else flash[:notice] = "Failed." #redirect_to pages_add_lesson_url end end def new @lesson=Lesson.new @lesson.build_sublesson end private def lesson_params params.require(:lesson).permit( :lesson_name, :lesson_icon, sublesson_attributes:[:sublesson_name, :sublesson_content, :sublesson_video_link]) end
I want to save those data in database but i don't succeed. i try a lot of ways....PLease tell me an option..Thanks(i'm running rails 4.1.1 and ruby 2.0.0)