邪恶的gem与茧gem和设计用户模型

所以我有一个User devise生成的User模型,我正在使用Wicked gem为我提供多forms选项,从用户那里获取更多数据并将其存储在用户模型中。

一切都工作正常,但知道我正在尝试添加另一个用户有很多学位的模型。 我正在使用cocoon gem让我增加额外的degrees 。 我可以进入多forms页面并输入学位信息,甚至可以向用户添加更多学位,但是当我提交表单时,我收到错误;

 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(这是邪恶的gem控制器):

 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 

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

  

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

  

终端日志:

 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' 

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


您遇到的问题是您的表单提交不包含任何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的呈现方式和传递方式。

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

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