2016-12-06 4 views
0

Ich habe die folgenden Ressourcen definiert:verschachtelte Ressource nur auf bearbeiten arbeiten nicht neu, Schienen 5.0.0.1

resources :buildings do 
    resources :buildings_regular_hours 
end 

Meine Modelle sind folgende:

class Building < ApplicationRecord 
    has_many :building_regular_hours 

    def to_s 
    name 
    end 
end 

class BuildingsRegularHours < ApplicationRecord 
    belongs_to :building 
end 

Ich versuche, ein Formular zu erstellen um das Erstellen und Bearbeiten von BuildingRegularHours zu ermöglichen. Momentan wird das Formular, das ich habe, auf #edit angezeigt, aber nicht auf #new angezeigt.

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 

Nachdem ich über die Konsole BuildingRegularHours hinzugefügt habe, habe ich versucht, die #edit Aktion, und es funktioniert einwandfrei, das Formular wie erwartet angezeigt. Allerdings, wenn ich die #new Aktion versuchen, erhalte ich folgende Fehlermeldung:

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 

Ich stelle fest, dass ich richtig die Ressourcen in der form_for Tag verschachtelt sind, dass beide @building und @building_regular_hour durch die Steuerung eingestellt werden, und dass ich Ich rufe das Formular in genau der gleichen Weise für beide #edit und #new. Dies ist alles, was ich bisher tun musste, um verschachtelte Ressourcen zu nutzen, also bin ich etwas verloren, was ich als nächstes tun soll.

Bitte beachten Sie, dass ich noch nicht versucht habe, das Formular zu bearbeiten - ich weiß, dass dort Arbeit zu erledigen ist. Ich versuche gerade, #new zu erhalten, um das Formular anzuzeigen.

Antwort

0

Sie müssen den Verein

class Building < ApplicationRecord 
    has_many :buildings_regular_hours 

    def to_s 
    name 
    end 
end 

class BuildingsRegularHour < ApplicationRecord 
    belongs_to :building 
end 

Ihr Modellname sollte immer singulär korrigieren BuildingsRegularHour oder es wird schaffen Probleme mit Routen und Verbände

+0

den Modellnamen ändern und die Modelldatei Umbenennung hat das nicht tun Trick, aber ich bin mir immer noch nicht sicher, dass dies nicht die Antwort ist. Ich werde sauber anfangen, da ich früh genug in der Entwicklung bin und keine Mehrzahl verwende. –

+0

gehen Sie einfach durch Führungen einmal –

+0

Das Lesen der Führungen für eine halbe Stunde wird Stunden des Debuggens für Sie speichern –

Verwandte Themen