编辑嵌套属性/模型轨道

时间:2018-04-27 05:04:31

标签: ruby-on-rails ruby-on-rails-5 nested-attributes

您好我正在使用@jordanhudgens嵌套属性教程构建一个rails应用程序来处理@sites和@site_assets。我可以添加新的@site_assets并删除它们,但我很难编辑它们。

当我尝试编辑时出现以下错误

ArgumentError in Sites::SiteAssets#edit
Showing app/views/sites/site_assets/_form.html.erb where line #1 raised:

First argument in form cannot contain nil or be empty
Extracted source (around line #1):

<%= form_for [@site, @site_asset] do |form| %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :site_asset_name %>
  </div>

Trace of template inclusion: app/views/sites/site_assets/edit.html.erb

显示Site_Asset循环的网站显示页面,包括编辑按钮

<tbody>
  <% @site_assets.each do |site_asset| %>
    <tr>
      <td><%= site_asset.name %></td>
      <td><%= site_asset.asset_type.name %></td>
      <td><%= site_asset.manufacturer.name %></td>
      <td><%= site_asset.model %></td>
      <td><%= site_asset.serial %></td>
      <td><%= site_asset.location.name %></td>
      <td><%= site_asset.installed %></td>
      <td><%= site_asset.condition %></td>
      <td><%= link_to 'Edit', edit_site_site_asset_path(site_id: @site.id, id: site_asset.id) %></td>
      <td><%= link_to 'Delete', [@site, site_asset], method: :delete, confirm: 'are you sure?'%></td>
    </tr>
  <% end %>
  </tbody>

视图/位点/ site_assets / _form.erb.html

<%= form_for [@site, @site_asset] do |form| %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :site_asset_name %>
  </div>
  <div class="field">
    <%= form.label :manufacturer_id %>
    <%= form.collection_select :manufacturer_id, Manufacturer.all, :id, :name %>
  </div>

  <div class="field">
    <%= form.label :model %>
    <%= form.text_field :model, id: :site_asset_model %>
  </div>

  <div class="field">
    <%= form.label :serial %>
    <%= form.text_field :serial, id: :site_asset_serial %>
  </div>

  <div class="field">
    <%= form.label :location_id %>
    <%= form.collection_select :location_id, Location.all, :id, :name %>
  </div>

  <div class="field">
    <%= form.label :asset_type_id %>
    <%= form.collection_select :asset_type_id, AssetType.all, :id, :name %>
  </div>

  <div class="field">
    <%= form.label :installed %>
    <%= form.date_select :installed, id: :site_asset_installed, class: 'datepicker' %>
  </div>

  <div class="field">
    <%= form.label :condition %>
    <%= form.select :condition, SiteAsset.conditions.keys %>
  </div>


  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Site_Asset模型

class SiteAsset < ApplicationRecord
  belongs_to :site
  belongs_to :asset_type
  belongs_to :location
  belongs_to :manufacturer

  enum condition: { New: 4, Good: 3, Ok: 2, Poor: 1 }
end

控制器/现场/ Site_Assets_Controller

class Sites::SiteAssetsController < ApplicationController

  # GET /site_assets
  # GET /site_assets.json
  def index
    @site_assets = SiteAsset.all
  end

  # GET /site_assets/1
  # GET /site_assets/1.json
  def show
  end

  # GET /site_assets/new
  def new
    @site = Site.find(params[:site_id])
    @site_asset = SiteAsset.new
  end

  # GET /site_assets/1/edit
  def edit
  end

  # POST /site_assets
  # POST /site_assets.json
  def create
    @site = Site.find(params[:site_id])
    @site_asset = SiteAsset.new(site_asset_params)
    @site_asset.site = @site

    respond_to do |format|
      if @site_asset.save
        format.html { redirect_to @site, notice: 'Site asset was successfully created.' }
        format.json { render :show, status: :created, location: @site_asset }
      else
        format.html { render :new }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /site_assets/1
  # PATCH/PUT /site_assets/1.json
  def update

    @site = Site.find(params[:site_id])
    @site_asset = SiteAsset.find(params[:id])

    respond_to do |format|
      if @site_asset.update(site_asset_params)
        format.html { redirect_to @site, notice: 'Site asset was successfully updated.' }
        format.json { render :show, status: :ok, location: @site_asset }
      else
        format.html { render :edit }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /site_assets/1
  # DELETE /site_assets/1.json
  def destroy
    @site = Site.find(params[:site_id])
    @site_asset = SiteAsset.find(params[:id])

    @site_asset.destroy
    respond_to do |format|
      format.html { redirect_to @site, notice: 'Site asset was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_site_asset
      @site_asset = SiteAsset.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def site_asset_params
      params.require(:site_asset).permit(:name, :model, :serial, :installed, :site_id, :manufacturer_id, :asset_type_id, :location_id, :condition)
    end
end

正如我所说,创建和删除方法都完美无缺。这只是我的编辑

1 个答案:

答案 0 :(得分:1)

您需要在编辑方法

中添加以下行
@site = Site.find(params[:site_id])
@site_asset = SiteAsset.find(params[:id])

渲染编辑表单时,它需要@site和@site_assets,但它收到nil。这就是它显示上述错误的原因。