【发布时间】:2014-04-11 00:04:37
【问题描述】:
我正在制作一个简单的应用程序,用户可以在其中创建一个系列,然后为该系列创建一个剧集,然后为每个剧集创建多个链接。我尝试使用 gem Cocoon,但无法让它显示在视图中。
我以为我已经把一切都做好了,但我希望有人能帮我找出我做错了什么或遗漏了什么,谢谢!
我收到此错误:
param is missing or the value is empty: series
在控制台中:
Processing by SeriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KAF06O/2C3EBRwos7UnJGSzWF2SGVVB7YdrNnuWt0M=", "commit"=>"Update Series", "id"=>"2"}
Series Load (0.2ms) SELECT "series".* FROM "series" WHERE "series"."id" = ? LIMIT 1 [["id", "2"]]
Completed 400 Bad Request in 39ms
ActionController::ParameterMissing (param is missing or the value is empty: series):
app/controllers/series_controller.rb:64:in `series_params'
app/controllers/series_controller.rb:35:in `block in update'
app/controllers/series_controller.rb:34:in `update'
这些是我的模型的样子:
# app/models/series.rb
class Series < ActiveRecord::Base
has_many :episodes
accepts_nested_attributes_for :episodes
end
# app/models/episode.rb
class Episode < ActiveRecord::Base
belongs_to :series
has_many :links
accepts_nested_attributes_for :links
end
# app/models/link.rb
class Link < ActiveRecord::Base
belongs_to :episode
end
我的控制器:
class SeriesController < ApplicationController
before_action :set_series, only: [:show, :edit, :update, :destroy, :links]
def new
@series = Series.new
@series.episodes.build
end
def update
respond_to do |format|
if @series.update(series_params)
format.html { redirect_to @series, notice: 'Series was successfully updated.' }
format.json { render :show, status: :ok, location: @series }
else
format.html { render :edit }
format.json { render json: @series.errors, status: :unprocessable_entity }
end
end
end
# ... ignoring content that hasn't changed from scaffold
def links
@episodes = @series.episodes
end
private
def series_params
params.require(:series).permit(:name,
episodes_attributes: [:id, :title,
links_attributes: [:id, :url]
])
end
end
视图文件:
<!-- app/views/series/links.html.erb -->
<h1><%= @series.name %></h1>
<%= form_for(@series) do |f| %>
<table>
<thead>
<tr>
<td>Title</td>
<td>Season</td>
<td>Episode</td>
</tr>
</thead>
<tbody>
<% @episodes.each do |episode| %>
<tr>
<td><%= episode.title %></td>
<td><%= episode.season %></td>
<td><%= episode.episode %></td>
<td>
<%= f.fields_for :episodes, episode.build do |e| %>
<%= e.fields_for :links, episode.link.build do |a| %>
<%= a.text_area :url %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
还有路由文件:
MyApp::Application.routes.draw do
resources :series do
member do
get 'links'
end
end
end
【问题讨论】:
-
从 SeriesController 发布您的
update操作和导致异常的视图,视图中没有提供series但update操作正在使用series_params这就是导致例外 -
@bjhaid 我刚刚用信息更新了问题,请看一下
-
您可能希望将视图中的
@episodes更改为f.episodes,并将内部循环指向您将块绑定到的任何变量而不是f
标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms nested-attributes