ruby on rails-收到此错误:param丢失或值为空:series



我正在制作一个简单的应用程序,用户可以在其中创建一个系列,然后创建该系列的一集,然后创建每个集的多个链接。我试着用宝石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

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

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

请在您的视图文件中检查您是否传递了视图

你也需要建立你的链接:

 #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&create动作有效。但是,您发布了update操作的日志——您对createupdate有问题吗?

最新更新