Forum: Ruby on Rails Form Fields Database save

A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-08 09:15
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)
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-08 10:06
(Received via mailing list)
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| %>
> %></br>           <%= sublesson_a.label :sublesson_content, "Content"
>          <%=(1..50).each do |i|%>
> "form-control" %></br>
>       </div>
>         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
A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-08 10:27
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
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-08 10:31
(Received via mailing list)
See inside of your log file or terminal, may be you'll more details
there.
A551dd246cb624b7fd6a78d5ccee835d?d=identicon&s=25 Turcu Marius (mariusturcu)
on 2014-08-08 12:04
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?
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-08 13:10
(Received via mailing list)
As you can see, the problem comes from permitted params for sublesson in
your controller. Replace 'sublesson_attributes' by sublesson.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-08-10 15:08
(Received via mailing list)
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/w...

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. :)

--Matt Jones
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.