Ruby on Rails HTML problem

30 views
Skip to first unread message

Turcu Marius

unread,
Aug 10, 2014, 3:14:31 AM8/10/14
to rubyonra...@googlegroups.com
I have a form like this..
<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([ "ico-03",
"ico-04","ico-05","ico-06" ])%></br></br>

<div>
<%= f.fields_for :sublessons do |sublesson_a| %>
<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>
</div>
<%end%>
<a href="javascript:;" style="float:right" class="btn btn-primary"
id="addNewTag">Add Sublesson</a>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

In controller i have...(because i want 51 sublessons fields)
def new_lesson
@lesson=Lesson.new
51.times{@lesson.sublessons.build}
end

The problem is in html the sublessons fields look like this....
<div id="main" class="clearfix" style="overflow: hidden">
<br>
<div style="padding:13px"> </div>
<div style="padding:13px">
<form id="new_lesson" class="new_lesson" method="post" action="/lessons"
accept-charset="UTF-8">
<div style="display:none">
<label for="lesson_lesson_name">Lesson title</label>
<br>
<input id="lesson_lesson_name" class="form-control" type="text"
name="lesson[lesson_name]">
<br>
<label for="lesson_lesson_icon">Choise icon</label>
<select id="lesson_lesson_icon" name="lesson[lesson_icon]">
<br>
<br>
<div>
<div class="">
</form>
</div>
<div class="">
</div>
<div class="">
</div>
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">
<div class="add_new_subtitle">

This went i watch with firebug but when i see the source of the face
everything looks fine....I attach also a printscreen...Please help

Attachments:
http://www.ruby-forum.com/attachment/9972/Capture.JPG


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

Matt Jones

unread,
Aug 10, 2014, 9:29:35 AM8/10/14
to rubyonra...@googlegroups.com
This looks like an "invalid markup" issue. At the top of the section you've got:

<div>
  <%= f.fields_for :sublessons do |sublesson_a| %>
    <div class="add_new_subtitle">

but at the bottom you've got

    </div>
  </div>
<% end %>

So the first time around the loop, the second </div> there closes the surrounding <div> (the one before f.fields_for). On subsequent iterations, the </div> is invalid (the next enclosing element is a <form> tag, not a <div>) but most browsers will try to muddle through, causing weird rendering.

--Matt Jones
 

Walter Lee Davis

unread,
Aug 10, 2014, 10:58:52 AM8/10/14
to rubyonra...@googlegroups.com
Are you trying to build a nested form? There's a Railscast abut that, and it's also covered in the Rails Guides, I think. The thing is you cannot nest a form inside another form -- that's just basic HTML rules, nothing specific to Rails. To make a nested form, you need to declare it as such in your view. I see that you have a relationship set up between a lesson and a sub lesson, so you need to declare in that relationship that the lesson accepts_nested_attributes_for the sub lessons. Then your lesson form can contain sub lesson fields, and the whole thing will be submitted and processed in one action.

Walter

> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1f56e1471a679746c29429b2bedc5df2%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Turcu Marius

unread,
Aug 10, 2014, 2:01:45 PM8/10/14
to rubyonra...@googlegroups.com
walter thanks but i have you write....mat, you have right, you save my
day...i was watched that code 2 hours and i don't figure out....Thanks
all for trying to help me:)
Reply all
Reply to author
Forward
0 new messages