【问题标题】:Got this error: param is missing or the value is empty: series收到此错误:参数丢失或值为空:系列
【发布时间】: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 操作和导致异常的视图,视图中没有提供 seriesupdate 操作正在使用 series_params 这就是导致例外
  • @bjhaid 我刚刚用信息更新了问题,请看一下
  • 您可能希望将视图中的@episodes 更改为f.episodes,并将内部循环指向您将块绑定到的任何变量而不是f

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


【解决方案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

请检查您是否通过视图查看文件

【讨论】:

    【解决方案2】:

    你也需要建立你的链接:

     #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 %>
    

    这应该可以让newcreate 操作生效。但是,您发布了update 操作的日志——您是否遇到createupdate 的问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多