내 편집 및 업데이트 작업을 내 앱에서 수행하는 데 실제 문제가 있습니다. 프로젝트와 연결된 기술을 편집하고 싶습니다.레일 : collection_select (편집 양식)
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을 게재되지 않습니다. 누군가 제게 올바른 방향으로 나를 가르쳐 줄 수 있습니까? 나는 레일스에 익숙하지 않아서 내가 이해할 수없는 모든 단순한 것이 될 수있다. 미리 감사드립니다. 이 같은
컬렉션 선택을 시도한 적이 없으므로 같은지 확실하지는 않지만''selected => t.object.your_field_name' '옵션을 체크 아웃하면 정규 선택을 위해 데이터베이스의 항목을 강조 표시합니다. 이 [article] (http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails)도 좋은 자료입니다. – diasks2
params [: technols] .nil?'내가 제대로하고있다면,'selected => @ selected_tech'를'@selected_tech = params [: technols] [: id] .reject (& : blank? ? 페이지를 다시로드 할 때 아무 것도 바뀌지 않았습니다. –