出现此错误:param丢失或值为空:series

时间:2014-04-11 00:04:37

标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms nested-attributes

我正在创建一个简单的应用程序,用户可以在该系列中创建一个系列剧集,然后是剧集的剧集,然后是每个剧集的多个链接。我尝试使用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

2 个答案:

答案 0 :(得分:1)

我认为系列参数没有从您的视图中传递

Parameters: {"utf8"=>"✓", "authenticity_token"=>"KAF06O/2C3EBRwos7UnJGSzWF2SGVVB7YdrNnuWt0M=", "commit"=>"Update Series", "id"=>"2"}                               

在您的操作中,您正在使用series_params方法,因为您使用的是params.require(:series).permit

所以它正在抛出param is missing or the value is empty: series

请检查您传递视图的视图文件

答案 1 :(得分:0)

您还需要建立链接:

 #app/controllers/series_controller.rb
 def new
    @series = Series.new
    @series.episodes.build.links.build #-> creates one link object you can populate
 end

然后,您需要更改表单以使用构建的链接对象:

  <%= f.fields_for :episodes do |e| %>
      <%= e.fields_for :links do |a| %>
        <%= a.text_area :url %>
      <% end %>
    <% end %>

这应该得到new&amp; create行动起作用。但是,您发布了update操作的日志 - 您是否遇到createupdate的问题?

相关问题