Form Fields Database save

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)

<%= form_for @lesson do |f| %> <%= f.label :lesson_name, "Lesson title" %> <%= f.text_field :lesson_name, class: "form-control" %> <%= f.label :lesson_icon, "Choise icon" %> <%= f.select "lesson_icon", options_for_select([ "ico03", "ico04","ico05","ico06" ])%>
<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”
%> <%= 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” %>

     <%=(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” %>
<%= 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%>

    <%end%>
    <a  href="javascript:;" style="float:right" class="btn

btn-primary" id=“addNewTag”>Add Sublesson
<%= 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)

On Friday, August 8, 2014 9:16:39 AM UTC+2, Ruby-Forum.com User wrote:

class Sublesson < ActiveRecord::Base
<%= form_for @lesson do |f| %>
%>
<%= sublesson_a.label :sublesson_content, “Content”
<%=(1…50).each do |i|%>
“form-control” %>


redirect_to lessons_new_lesson_url
def lesson_params

Posted via http://www.ruby-forum.com/.

I should modify the way you build (initialize) the sublesson in #new
action
of the controller.
instead of calling:
@lesson.build_sublesson

you should do it as follows:

@lesson.sublessons.build #to build just one sublesson

".times { @lesson.sublessons.build} #to build just 3 sublessons

Still no efect…
I don’t know if this function is good…
def lesson_params
params.require(:lesson).permit(
:lesson_name, :lesson_icon, sublesson_attributes:[:sublesson_name,
:sublesson_content, :sublesson_video_link])
end

See inside of your log file or terminal, may be you’ll more details
there.

Parameters: {“utf8”=>“√”,
“authenticity_token”=>“25UUu0M740UaYjPKW2Vr5370TUn3s
eK/yUiYd/7PfYk=”, “lesson”=>{“lesson_name”=>“asdaas”,
“lesson_icon”=>“ico03”, “s
ublesson”=>{“sublesson_name”=>“asd”, “sublesson_content”=>“asd”,
“sublesson_vide
o_link”=>“asd”}}, “commit”=>“Submit”}
Unpermitted parameters: sublesson
(0.0ms) BEGIN
SQL (53.0ms) INSERT INTO lessons (lesson_icon, lesson_name)
VALUES (‘ic
o03’, ‘asdaas’)
(1.0ms) COMMIT
Redirected to http://localhost:3000/lessons/new_lesson

isn’t making the insert…but why?

On Friday, 8 August 2014 03:16:39 UTC-4, Ruby-Forum.com User wrote:

Hello! I have a little problem saving some data in database…
I have to models…
class Lesson < ActiveRecord::Base
has_many :sublessons, :dependent => :destroy

I don’t believe this is the code you’re actually running. The form below
refers to sublesson (no s), and the use of @lesson.build_sublesson
in
the controller also strongly suggests you’ve actually got a has_one :sublesson in your model.

Once you fix that, take a look at a good tutorial for nested attributes
+
forms; this one seems solid:

http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

Related: you’ll get better support if you can provide more detail than
“I
didn’t succeed”. Log messages, etc are hugely important. Also, when in
doubt try restarting the rails s process. :slight_smile:

–Matt J.

As you can see, the problem comes from permitted params for sublesson in
your controller. Replace ‘sublesson_attributes’ by sublesson.