在2个控制器之间使用form_for时出错(params缺失或为空)

时间:2015-03-27 09:39:34

标签: ruby-on-rails

我得到的错误是:

ActionController::ParameterMissing in CampaignsController#create
param is missing or the value is empty: campaign

我的路线:

resources :users, except: [:destroy] do
  post 'add_email', on: :member
end

resources :campaigns, only: [:create, :destroy, :index] do
  get 'status'
end

我在广告系列中显示的模式#index

<%= form_for add_email_user_path(@user) do |f| %>
 <%= f.email_field :email, placeholder: 'Your email address', class: 'input-lg form-control', autofocus: true %>
 <%= f.button 'Continue', class: 'btn btn-success', 'data-disable-with' => "Saving <i class='fa fa-spinner fa-spin'></i>".html_safe %>
<% end %>

campaigns_controller:

def index
  @campaigns = current_user.campaigns.all
  @new_campaign = current_user.campaigns.new
  @user = current_user
end

private

def campaign_params
  params.require(:campaign).permit(:keyword)
end

users_controller:

def add_email
  @user = User.find(params[:id])
  if @user.update_attributes(setup_params)
    redirect_to campaigns_path, notice: 'Thank you for adding your email!'
  else
    redirect_to :back, alert: 'Unable to save your email'
  end
end

private

def setup_params
  params.require(:user).permit(:email)
end

日志:

Started POST "/campaigns" for 127.0.0.1 at 2015-03-27 11:14:30 +0100
  ActiveRecord::SchemaMigration Load (0.6ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by CampaignsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1fdQs7J5vvEV/b29thmAx5udaTc7ushkDAutJPWSESc=", "/users/7/add_email"=>{"email"=>"sebe@asm.com"}, "follow"=>"yes", "button"=>""}
  User Load (1.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 7]]
Completed 400 Bad Request in 85ms

ActionController::ParameterMissing (param is missing or the value is empty: campaign):
  app/controllers/campaigns_controller.rb:42:in `campaign_params'
  app/controllers/campaigns_controller.rb:6:in `create'


  Rendered /Users/sja/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.4ms)
  Rendered /Users/sja/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
  Rendered /Users/sja/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
  Rendered /Users/sja/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (26.3ms)

所以我要做的是允许用户添加他们的电子邮件(在广告系列索引中显示模式,但由于某种原因,我得到了广告系列参数丢失。广告系列属于用户。

2 个答案:

答案 0 :(得分:0)

即使您正在尝试向用户添加电子邮件,您的日志也会以某种方式显示此操作调用来自活动的创建操作,并且参数完全错误,我怀疑这是因为您正在使用:

form_for

何时使用:

form_tag

使用form_for会查找资源,因为您在/campaings,它会将此操作解释为广告系列的帖子。

或者,如果您想使用form_for,则应将用户添加到表单中:

<%= form_for @user, url: add_email_user_path(@user) do |f| %>

请参阅此link了解form_forform_tag

之间的差异

此外,您只是使用电子邮件更新您的用户,因此您可以使用默认的更新操作表单用户控制器并删除add_email方法。

<%= form_for @user do |f| %>
  <%= f.email_field :email, placeholder: 'Your email address', class: 'input-lg form-control', autofocus: true %>
  <%= f.submit 'Continue', class: 'btn btn-success', 'data-disable-with' => "Saving <i class='fa fa-spinner fa-spin'></i>".html_safe %>
<% end %>

答案 1 :(得分:0)

form_for [:add_email, @user], method: :post do |f|

或更长的版本:

form_for @user, url: add_email_user_path(@user), method: :post do |f|