Wicked Gem with cocoon gem and devise user model

时间:2016-02-08 13:22:07

标签: ruby-on-rails ruby-on-rails-4 devise cocoon-gem wicked-gem

所以我使用User生成了devise模型,我使用Wicked gem为我提供了多种形式的选项,可以从用户那里获取更多数据并将其存储在用户模型中

一切都运转正常,但我知道我试图在用户拥有多个学位的地方添加另一个模型。我正在使用cocoon gem来允许我添加额外的degrees。我可以进入多形式页面并输入学位信息,甚至可以向用户添加更多学位,但是当我提交表单时,我收到错误;

param is missing or the value is empty: user

其他所有内容实际上都会被保存,我可以查看用户和输入的其他字段但不包含度数。

用户模型:

has_many :degrees
accepts_nested_attributes_for :degrees, reject_if: :all_blank, allow_destroy: true

学位模型:

belongs_to :user

user_steps_controller(这是邪恶的宝石控制器):

class UserStepsController < ApplicationController

  include Wicked::Wizard
  steps :personal, :avatar_and_about_yourself, :social, :education

  def show
    @user = current_user
    render_wizard
  end

  def update
    @user = current_user
    @user.update_attributes(user_params)
    render_wizard @user
  end

  private

  def user_params
    params.require(:user).permit(:name, :middlename, :lastname, :avatar, :aboutMe, :twitterlink, :githublink, :stackoverflowlink, :mediumlink, :dribblerlink, degrees_attributes: [:id, :degreeName, :university, :level, :done, :_destroy])
  end

end

注册控制器(用于设计):

class Users::RegistrationsController < Devise::RegistrationsController
# before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
   def new
     super
   end

  # POST /resource
   def create
     super
   end

  # PUT /resource
   def update
     super
   end

   protected

  # The path used after sign up.
   def after_sign_up_path_for(resource)
     user_steps_path
   end

  # The path used after sign up for inactive accounts.
   def after_inactive_sign_up_path_for(resource)
     user_steps_path
   end

   def sign_up_params
     params.require(:user).permit(:email, :password, :password_confirmation, :name, :middlename, :lastname, :avatar, :aboutMe, :twitterlink, :githublink, :stackoverflowlink, :mediumlink, :dribblerlink, degrees_attributes: [:id, :degreeName, :university, :level, :done, :_destroy])
   end
end

我在user_steps_controller中收到错误:

ActionController::ParameterMissing in UserStepsController#update
param is missing or the value is empty: user

并且错误在此行内:

 def user_params
    params.require(:user).permit(:name, :middlename, :lastname, :avatar, :aboutMe, :twitterlink, :githublink, :stackoverflowlink, :mediumlink, :dribblerlink, degrees_attributes: [:id, :degreeName, :university, :level, :done, :_destroy])
  end

另外,我将如何查看输入的字段,例如,如果我想查看用户名:

<%= @user.name %>

但我如何展示每个学位?它只是一个循环吗?

<% @users.degree.each do |degree| %>

终端日志:

Started PATCH "/user_steps/education" for ::1 at 2016-02-09 11:23:29 +0000
Processing by UserStepsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"EXN7ts6xPVK8vkC7q6UcNleJJWLUtKmOw41T0qsDqXCrPJ0vIHrB/6xIfpp/o+cXKR47F+6LxUY5LjXlCobKZQ==", "commit"=>"Update User", "id"=>"education"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 16]]
Completed 400 Bad Request in 2ms (ActiveRecord: 0.2ms)

ActionController::ParameterMissing (param is missing or the value is empty: user):
  app/controllers/user_steps_controller.rb:20:in `user_params'
  app/controllers/user_steps_controller.rb:13:in `update'

1 个答案:

答案 0 :(得分:1)

这个评论太长了,我完全希望能够更新它。

您遇到的问题是您的表单提交包含任何user参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"EXN7ts6xPVK8vkC7q6UcNleJJWLUtKmOw41T0qsDqXCrPJ0vIHrB/6xIfpp/o+cXKR47F+6LxUY5LjXlCobKZQ==", "commit"=>"Update User", "id"=>"education"}

这可能是Wicked,但是从RegistrationsController的虚假使用来判断,我想它可能与格式化等有关:

-

如果您要更新current_user,则应该调用您的registrations控制器。那是注册 - 你应该使用你的users控制器:

#config/routes.rb
resource :user_steps, path: "user", only: [:show, :update] #-> url.com/user (singular resource)

#app/controllers/user_steps_controller.rb
class UserStepsController < ApplicationController
  def show
  end

  def update
    @user = current_user.update user_params
    render_wizard @user
  end

  private

  def user_params
    params.require(:user).permit(:name, :middlename, :lastname, :avatar, :aboutMe, :twitterlink, :githublink, :stackoverflowlink, :mediumlink, :dribblerlink, degrees_attributes: [:id, :degreeName, :university, :level, :done, :_destroy])
  end
end

这只是控制器;你需要看看form是如何呈现的&amp;通过。

如果您使用上述代码发送数据,则需要提取form HTML并显示Wicked如何使用它。

您需要在app/views/users/show.html.erb中查找要显示的表单,不是 /app/views/registrations