많은 realtions가있는 하나의 처방전을 만드는 매우 복잡한 형식을 구축했습니다. 내가보기에이 구문을 사용하고 있습니다 :레일 4 중첩 된 형식의 해시에 액세스 할 수 없습니다 (정의되지 않은 메서드 인 [nilClass의 경우 []])
- provide(:title, 'Create prescription')
%h1 Add medicines to prescription
.row
.span6.offset3
= form_for @prescription do |f|
= render 'shared/error_prescription_messages'
%p
= f.hidden_field :patient_id, :value => params[:patient_id]
= f.hidden_field :user_id, :value => current_user.id
= f.fields_for :relations do |builder|
= render 'child_form', :f => builder
%p= f.submit "Submit"
chlid_form은 매우 간단합니다 :
- it=f.options[:child_index].to_i
- n= it.to_s
%h2
= "Medicine ##{it+1}"
= f.hidden_field :medicine_id, :id => "my_medicine_id#{it}"
- if params[:prescription].nil? || params[:prescription][:relations_attributes][n.to_sym][:medicine_name].nil?
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}"
- else
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}", :value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
= f.label :amount, "Amount of medicine boxes"
= f.number_field :amount, :value => 1
= f.label :daily
= f.number_field :daily, :value => 1
= f.label :period_in_days, "Duration of treatment (in days)"
= f.number_field :period_in_days, :value => 1
을 그래서 당신은 내가 아이의 인덱스 (0, 1, 2를 얻을 수 f.options[:child_index]
을 사용하고 있습니다 볼 수 .. .)이 특정 양식으로 여러 항목을 생성합니다.
:value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
이 n
단지이다 : 그것은이 라인에서 작동하지 않지만 그때 완벽하게 정상적으로 작동 :id_element => "#my_medicine_id#{it}"
에 성공적 변수 it
및 사용에 넣어 (... my_medicine_id0, my_medicine_id1를 생성) n=it.to_s
.
컨트롤러에서 뭔가 잘못되었지만이 라인을 무엇이든 변경하면 :value => params[:prescription][:relations_attributes]**[:'0']**[:medicine_name]
또는 0에서 4까지의 다른 정수가 모두 작동하지만 위의 동적 변경이 필요합니다. 그래서 여기서 정수를 생성하기 때문에 그것이 작동한다는 증거가 있습니다 "#my_medicine_id#{it}"
하지만 해시에서는 작동하지 않습니다! 하지 않습니다
{"patient_id"=>"7", "user_id"=>"1", "relations_attributes"=>{"0"=>{"medicine_id"=>"13490", "medicine_name"=>"Locacid 500 mcg/g (0,05%) (1 tuba 30 g)", "amount"=>"0", "daily"=>"1", "period_in_days"=>"1"}, "1"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "2"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "3"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "4"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}}}
params[:prescription][:relations_attributes][SOME_KIND_OF_INETEGER][:medicine_name]
작동합니다 꽤 분명 필요한 값을 얻을 수 있지만 : 나는 PARAMS에서 전체 해시를 인쇄 할 때 내가이 얻을.
컨트롤러 코드 :
class PrescriptionsController < ApplicationController
before_action :signed_in_user
before_action :doctor_user, only: [:new, :create]
before_action :pharmacist_user, only: [:update]
def new
@prescription =Prescription.new
5.times { @prescription.relations.build }
end
def create
@prescription = Prescription.new(new_prescription_params)
if @prescription.save
flash[:success] = "Prescription created."
redirect_to @prescription
else
5.times { @prescription.relations.build }
render 'new', :prescription => params[:prescription]
end
end
def show
@prescription = Prescription.find(params[:id])
@medicines = @prescription.medicines.paginate(page: params[:page], :per_page => 10)
end
def update
@prescription = Prescription.find(params[:id])
@patient = Patient.find(params[:patient_id])
if !prescription_expired?(@prescription)
@prescription.realized = 1
if @prescription.save
flash[:success] = "Prescription realized."
redirect_to @patient
else
redirect_to root_url
end
else
flash[:notice] = "Can't realize, prescription expired."
redirect_to @patient
end
end
private
def new_prescription_params
params.require(:prescription).
permit(:patient_id, :user_id, relations_attributes: [:medicine_id, :medicine_name, :amount, :daily, :period_in_days])
end
def doctor_user
redirect_to(root_url) unless current_user.function == "doctor"
end
def pharmacist_user
redirect_to(root_url) unless current_user.function == "pharmacist"
end
def prescription_expired?(presc)
presc.created_at < 1.month.ago
end
def signed_in_user
unless signed_in?
store_location
flash[:notice] = "Please log in."
redirect_to login_url
end
end
끝
사람이 도울 수 있다면 내가 너희들에게 내가 아이디어의 부족. 감사.
이렇게하는 것은 레일 방식으로는 보이지 않습니다. 양식 요소를 모델과 바인딩 한 다음이 속성 값을 무시하는 이유는 무엇입니까? 이 작업은 컨트롤러에서 수행해야합니다. 컨트롤러 코드로 질문을 업데이트하여 정리하십시오. – BroiSatse
나는 왜 당신이'[n.to_sym]'을하고 있는지 확신하지 못합니다.''[n]'이 효과가있을 것이라고 생각합니까? 해시 덤프의 상징이 아닙니다. – SteveTurczyn
개인적인 것을 쓰지 마십시오. 그들이 좌절했다는 주장을 읽는 것은 실망 스럽습니다. 그것은 그 질문과 아무런 관련이 없습니다. 그리고 초보자는 "기이하고 이상한 행동"과 같은 단어를 쓰곤하지만, 대부분의 경우 언어/프레임 워크에 이상하거나 이상한 점은 없으며 이상하다고 생각하는 사람들이 생각하는 것일 가능성이 큽니다. – sawa