【问题标题】:nested resource only working on edit not new, rails 5.0.0.1嵌套资源仅适用于编辑不是新的,rails 5.0.0.1
【发布时间】:2017-04-21 12:37:09
【问题描述】:

我定义了以下资源:

resources :buildings do
  resources :buildings_regular_hours
end

我的模型如下:

class Building < ApplicationRecord
  has_many :building_regular_hours

  def to_s
    name
  end
end

class BuildingsRegularHours < ApplicationRecord
  belongs_to :building
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 操作,它工作得很好,按预期显示了表单。但是,当我尝试 #new 操作时,我收到以下错误:

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 标记中,@building@building_regular_hour 均由控制器设置,并且我以完全相同的方式调用表单 @987654337 @ 和 #new。这是我之前必须做的所有事情才能使嵌套资源工作,所以我有点不知所措。

请注意,我还没有尝试使表单工作 - 我知道那里有工作要做。我只是想让#new 显示表单。

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-5 nested-resources


    【解决方案1】:

    您需要更正关联

    class Building < ApplicationRecord
      has_many :buildings_regular_hours
    
      def to_s
        name
      end
    end
    
    class BuildingsRegularHour < ApplicationRecord
      belongs_to :building
    end
    

    您的模型名称应始终为单数 BuildingsRegularHour,否则会产生路由和关联问题

    【讨论】:

    • 更改模型名称和重命名模型文件并没有解决问题,但我仍然不确定这不是答案。我要从头开始,因为我的开发还很早,而且不使用复数形式。
    • 只需通过 rails guides 一次
    • 阅读指南半小时将为您节省数小时的调试时间
    • 我已阅读指南并理解它们。我以前部署过带有嵌套资源的 Rails 应用程序。我只是从未尝试使用使用 --force-plurals 选项生成的模型,这可能是问题所在。我使用 --force-plurals 选项来尝试获得更有意义的模型名称,因为我正在建模一组小时;称他们为小时是不雅的,我认为您可以干净地覆盖复数选项。显然不是。感谢您假设我的知识为零。
    • 在一个全新的实例中,在不强制使用复数的情况下,我的实现是有效的。所以你是正确的,复数是问题。让我想知道为什么生成器有 --force-plurals 选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多