레일 :중첩 된 자원 만 편집 새로운 것이 아니다 작업 나는 다음과 같은 자원을 정의 5.0.0.1
class Building < ApplicationRecord
has_many :building_regular_hours
def to_s
name
end
end
class BuildingsRegularHours < ApplicationRecord
belongs_to :building
end
내가 양식을 만들려고 오전 :
resources :buildings do
resources :buildings_regular_hours
end
내 모델
은 다음과입니다BuildingRegularHours
을 만들고 편집 할 수 있습니다. 현재 양식은
#edit
에 표시되지만
#new
에는 표시되지 않습니다.
new.html.erb :
<%= render 'form', buildings_regular_hour: @buildings_regular_hour, building: @building %>
edit.html.erb :
<h1>Editing Buildings Regular Hour</h1>
<%= render 'form', buildings_regular_hour: @buildings_regular_hour, building: @building %>
<%= link_to 'Back', building_buildings_regular_hour_path(@building) %>
_form.html.erb :
<%= form_for([building,buildings_regular_hour]) do |f| %>
<% if buildings_regular_hour.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
<ul>
<% buildings_regular_hour.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :building_id %>
<%= f.text_field :building_id %>
</div>
<div class="field">
<%= f.label :start_date %>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :end_date %>
<%= f.date_select :end_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
buildings_regular_hours_controller.rb :
class BuildingsRegularHoursController < ApplicationController
before_action :set_buildings_regular_hour, only: [:show, :edit, :update, :destroy]
before_action :set_building
# GET /buildings_regular_hours
# GET /buildings_regular_hours.json
def index
@buildings_regular_hours = BuildingsRegularHours.all
end
# GET /buildings_regular_hours/1
# GET /buildings_regular_hours/1.json
def show
end
# GET /buildings_regular_hours/new
def new
@buildings_regular_hour = BuildingsRegularHours.new
end
# GET /buildings_regular_hours/1/edit
def edit
end
# POST /buildings_regular_hours
# POST /buildings_regular_hours.json
def create
@buildings_regular_hour = BuildingsRegularHours.new(buildings_regular_hour_params)
respond_to do |format|
if @buildings_regular_hour.save
format.html { redirect_to @buildings_regular_hour, notice: 'Buildings regular hours was successfully created.' }
format.json { render :show, status: :created, location: @buildings_regular_hour }
else
format.html { render :new }
format.json { render json: @buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /buildings_regular_hours/1
# PATCH/PUT /buildings_regular_hours/1.json
def update
respond_to do |format|
if @buildings_regular_hour.update(buildings_regular_hour_params)
format.html { redirect_to @buildings_regular_hour, notice: 'Buildings regular hours was successfully updated.' }
format.json { render :show, status: :ok, location: @buildings_regular_hour }
else
format.html { render :edit }
format.json { render json: @buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# DELETE /buildings_regular_hours/1
# DELETE /buildings_regular_hours/1.json
def destroy
@buildings_regular_hour.destroy
respond_to do |format|
format.html { redirect_to buildings_regular_hours_index_url, notice: 'Buildings regular hours was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_buildings_regular_hour
@buildings_regular_hour = BuildingsRegularHours.find(params[:id])
end
def set_building
@building = Building.find(params[:building_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def buildings_regular_hour_params
params.require(:buildings_regular_hour).permit(:building_id, :start_date, :end_date, :sunday_id, :monday_id, :tuesday_id, :wednesday_id, :thursday_id, :friday_id, :saturday_id)
end
end
콘솔을 통해 BuildingRegularHours
을 추가 했으므로 #edit
작업을 시도했지만 제대로 작동하여 예상대로 양식을 표시합니다. @building
및 @building_regular_hour
모두가 컨트롤러에 의해 설정되어 있는지,
Showing /Users/shawn/Documents/uga/library_hours/app/views/buildings_regular_hours/_form.html.erb where line #1 raised:
undefined method `building_buildings_regular_hours_index_path` for #<#<Class:0x007fe9589e2890>:0x007fe95f9bbb30>
Did you mean? building_buildings_regular_hours_path
building_buildings_regular_hour_path
building_buildings_regular_hours_url
building_buildings_regular_hour_url
Extracted source (around line #1):
1 <%= form_for([building,buildings_regular_hour]) do |f| %>
2 <% if buildings_regular_hour.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
5
6 <ul>
Trace of template inclusion: app/views/buildings_regular_hours/new.html.erb
내가 제대로 form_for
태그에 자원을 중첩 한 점에 유의하고, 그 I : 나는 #new
조치를 시도 할 때, 나는 다음과 같은 오류가 발생합니다 #edit
과 #new
모두 정확히 같은 방식으로 양식을 호출합니다. 이것은 이전에 중첩 된 리소스가 작동하도록하기 위해 수행해야했던 모든 작업이므로 다음에 수행해야 할 작업으로 손실이 조금 있습니다.
양식을 아직 만들지 않으 셨습니다. 거기서 할 일이 있다는 것을 알고 있습니다. 양식을 표시하기 위해 #new
을 얻으려고합니다.
모델의 이름을 변경하고하지 않았다 모델 파일의 이름을 변경 문제를 만들 것 트릭,하지만 난 아직도 이것이 대답이 아니라고 확신하지 못합니다. 나는 충분히 일찍 개발하고 복수형을 사용하지 않기 때문에 깨끗하게 시작할 것입니다. –
한 번만 레일 가이드를 통과하십시오 –
30 시간 가이드를 읽으면 몇 시간의 디버깅 시간을 절약 할 수 있습니다 –