0
좋아 나의 협회는 다음과 같습니다레일 3.2 - 중첩 된 자원 전달 ID
Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet
내 경로 :
resources :outlets do
resources :monitorings
end
보기 :
<%= link_to new_outlet_monitoring_path(@outlet) %>
내가 링크를 클릭 로그 outlet_id가 매개 변수로 새 페이지에 올바르게 전달되었음을 보여줍니다. 그러나 모니터링 레코드를 저장할 때 outlet_id는 0이됩니다.
어떤 도움이 필요합니까?
UPDATE :
# views/monitorings/_form.html.erb
<%= form_for(@monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
<div class="radio-group">
<%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
<%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
<%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
</div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>
그리고 컨트롤러는 :
# controllers/monitoring_controller.rb
def new
@monitoring = Monitoring.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @monitoring }
end
end
def create
@monitoring = Monitoring.new(params[:monitoring])
respond_to do |format|
if @monitoring.save
format.html { redirect_to @monitoring, notice: 'Monitoring was successfully created.' }
format.json { render json: @monitoring, status: :created, location: @monitoring }
else
format.html { render action: "new" }
format.json { render json: @monitoring.errors, status: :unprocessable_entity }
end
end
end
원래 질문에 대한 업데이트로 추가했습니다. – Ammar
두 가지 방법이 있습니다. 둘 다 새 컨트롤러 작업에서 콘센트 개체를 만드는 것이 포함됩니다. 그래서'@outlet = Outlet.find (params [: outlet_id])'를 새로운 액션에 추가하십시오. 이제 두 가지 옵션이 있습니다. 1 : @monitoring의 선언을 @monitoring = @ outlet.monitorings.new'로 변경하고 아무 것도 변경하지 마십시오. 2 : @monitoring 선언 만 남겨두고 form_for를'form_for [@outlet, @monitoring] do | f |'로 변경하십시오. – JohnColvin
옵션 2를 사용하고 이전에 옵션 1을 시도하지 않았습니다. 나는 ** 그것이 작동 할 것이라고 생각한다. – JohnColvin