param丢失或值为空:subscribeduser

时间:2016-07-29 14:26:15

标签: ruby-on-rails ruby

The Error I Get

当我尝试创建一个新的subscribeduser时出现错误,我有点"修复了"通过将params.require(:subscribeduser).permit(:email)转换为params.permit(:email),但我对此感觉不对。我如何正确摆脱这个错误?

SubscribedusersController

class SubscribedusersController < ApplicationController
  before_action :set_subscribeduser, only: [:show, :edit, :update, :destroy]

  def index
    @subscribedusers = Subscribeduser.all
  end

  def new
    @subscribeduser = Subscribeduser.new
  end

  def create
    @subscribeduser = Subscribeduser.new(subscribeduser_params)

    respond_to do |format|
      if @subscribeduser.save
        RegistrationMailer.thankyou_mail(@subscribeduser).deliver_now
        format.html { redirect_to @subscribeduser, notice: 'Subscribeduser was successfully created.' }
        format.json { render :show, status: :created, location: @subscribeduser }
      else
        format.html { render :new }
        format.json { render json: @subscribeduser.errors, status: :unprocessable_entity }
      end
    end
  end

  def subscribeduser_params
    params.require(:subscribeduser).permit(:email)
  end
end

这是Subscriuser表单

 <%= form_for(subscribeduser,:html => {:id => 'signup-form' }) do |f| %>

  <div class="field">
   <%= f.text_field :email, :name =>"email", :id => "signup-form", "placeholder" => "Email Address" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign Up!" %>
  </div>
 <% end %>

This Is What My Console Shows

2 个答案:

答案 0 :(得分:1)

简而言之,这意味着您要求param订阅用户,但您的控制器操作未收到用户。

如果没有从控制台看到你的视图代码或你的参数,很难准确地说出来,但是看看你的控制台输出并检查发送给控制器的参数创建动作。

编辑(您现在已经给出了控制台输出和视图):

如果您查看控制台输出,您将看到控制器所需的subscribeduser参数未在参数中被接收,因此创建操作失败。

答案 1 :(得分:0)

当然,您的表单不会呈现资源(订阅用户),我相信您错过了form_for中的@

试试这个form_for(@subscribeduser,:html => {:id => 'signup-form' })

基本上,当错误表明您在表单发布时缺少subscribeduser

我建议您阅读http://guides.rubyonrails.org/action_view_overview.html