我在尝试让我的编辑和更新操作适用于我的应用时遇到了实际问题。我想编辑与项目链接的技术。
def edit
@project = Project.find(params[:id])
@project_technol = @project.projecttechnols.build
end
def update
@project = Project.find(params[:id])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.role = params[:new_role] unless params[:new_role].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
项目模型:
class Project < ActiveRecord::Base
attr_accessible :fullname, :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benefits, :edited_date, :end_date, :entry_date,:industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech , :technols,
has_many :projecttechnols
has_many :technols, :through => :projecttechnols
accepts_nested_attributes_for(:technols)
当我进入编辑表单时,未在集合选择中选择先前选择的技术。
编辑表格
<%= f.fields_for(@project_technol) do |t| %>
<%= t.label "Choose Technologies"%> </br>
<%= t.collection_select(:id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
<% end %>
<% @project.technols.each do |t| %>
<li><%= t.tech %> <%= link_to "Details", technol_path(t), method: :delete, %></li>
<% end %>
我可以看到哪些技术使用@project.technols.each do |t|
循环,因此它们被找到,但它们没有显示collection_select。请有人指出我正确的方向。我是rails的新手,所以它可能是我不理解的每一个简单的东西。提前谢谢。
答案 0 :(得分:1)
尝试这样的事情:
<p><%= f.label :skills %><ul>
<% for skill in Skill.all %>
<li>
<%= check_box_tag "user[skill_ids][]", skill.id, @user.skills.include?(skill) %>
<%= skill.name %>
</li>
<% end %>
</ul>
</p>
从更新操作中删除代码
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
在项目模型中,您需要类似
的内容 enter code here
attr_accessible:technols_ids