[英]Rails 3 - How to set different id (html attribute) in nested form attributes
[英]Assigning dom id based on model attributes in Rails nested form
我有一個嵌套表格。 現在,我想用一些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 %>">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.