Здравствуйте, я использую учебник по вложенным атрибутам @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
Как я уже сказал, методы создания и удаления работают отлично. Это всего лишь редактирование, которое дало мне