我有一个嵌套的表单。现在我想安排一些CSS布局,但我面临的问题分配dom id的形式。
这是主题控制器。我想分配第5行所示的lesson_type作为dom id。
1 def index
2 @subjects = Subject.all
3 @subject = Subject.new
4 lecture = @subject.lessons.build
5 lecture.lesson_type = "lecture"
lecture.lesson_groups.build
lecture.destroy
tutorial = @subject.lessons.build
tutorial.lesson_type = "tutorial"
tutorial.lesson_groups.build
tutorial.destroy
laboratory = @subject.lessons.build
laboratory.lesson_type = "laboratory"
laboratory.lesson_groups.build
laboratory.destroy
respond_to do |format|
format.html # index.html.erb
format.json { render json: @subjects }
format.js
end
end
下面是表单。
<%= nested_form_for(@subject, :remote=>true) do |f| %>
<% if @subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% @subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject_code %><br />
<%= f.text_field :subject_code %>
</div>
<%= f.fields_for :lessons do |lesson| %>
<%= lesson.label :lesson_type %><br/>
<%= lesson.text_field :lesson_type, :readonly=>true%><br/>
<%= lesson.label :name %><br/>
<%= lesson.text_field :name %><br/>
<%= lesson.fields_for :lesson_groups do |lesson_group| %>
<%= lesson_group.label :group_index %><br/>
<%= lesson_group.text_field :group_index %>
<%= lesson_group.link_to_remove "Remove this task" %>
<% end %>
这是我想要添加id的div。
<%= f.fields_for :lessons do |lesson| %>
<%= lesson.label :lesson_type %><br/>
<%= lesson.text_field :lesson_type, :readonly=>true%><br/>
<%= lesson.label :name %><br/>
<%= lesson.text_field :name %><br/>
我尝试了下面的方法,但是没有成功。
<div id = "<%= :lesson_type%>">
如果有人能帮我,我会很感激的。
对不起……
#controller
def index
...
lecture.lesson_type = @lesson_dom_id = "lecture" # line 5
...
end
#view
<div id="<%= @lesson_dom_id %>">