邪恶宝石与茧宝石和设计用户模型 [英] Wicked Gem with cocoon gem and devise user model
问题描述
所以我有一个用户
模型生成与 devise
,我使用邪恶
gem给我多种选择,从用户那里获取更多数据并将其存储在用户模型中。
So I have a User
model generated with devise
, and I am using Wicked
gem to give me multiform option on taking more data from the user and storing it in the user model.
一切都正常,但知道我'尝试添加另一个模式,用户拥有多个学位。我正在使用茧宝石来允许我添加额外的度
。我可以去多页面,输入学位信息,甚至添加更多的学位,但是当我提交表单我收到一个错误;
Everything is working fine but know I'm trying to add another model where user has many degree. I am using cocoon gem to allow me to add extra degrees
. I am able to go to the multiform page and enter degree information and even add many more degrees to the user, but when i submit the form i get an error;
param is missing or the value is empty: user
获得保存,我可以查看用户和输入的其余字段,但不是度数。
Everything else actually gets saved am i can view the user and the rest of the fields entered but non of the degrees.
用户模型:
has_many :degrees
accepts_nested_attributes_for :degrees, reject_if: :all_blank, allow_destroy: true
度数模型:
belongs_to :user
user_steps_controller(这是恶魔宝石控制器):
user_steps_controller (this is the wicked gem 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
注册控制器(for devise):
Registration controller (for devise):
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中收到错误:
Im getting an error in the 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
另外,我该怎么去查看输入的字段例如,如果我想查看用户名:
Also how would i go and view the entered fields, for example if i wanted to view user name it is:
<%= @user.name %>
但我如何显示每个学位?它只是一个循环吗?
but how would i show each degree? is it just a loop?
<% @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'
推荐答案
这太久没有评论,我完全期望更新
This is too long to comment, I fully expect to update it.
您的问题是您的表单提交不 包含任何 user
params:
The problem you have is that your form submission does not include any user
params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EXN7ts6xPVK8vkC7q6UcNleJJWLUtKmOw41T0qsDqXCrPJ0vIHrB/6xIfpp/o+cXKR47F+6LxUY5LjXlCobKZQ==", "commit"=>"Update User", "id"=>"education"}
这可能是 Wicked
,但判断您的虚假使用注册控制器
,我想象可能是t o使用格式化等:
This might be Wicked
, but judging form your spurious use of RegistrationsController
, I'd imagine it could be to do with formatting etc:
-
如果您要更新您的 current_user
,您应该不调用注册
控制器。这是针对注册 - 您应该使用用户
控制器:
If you're updating your current_user
, you should not be invoking your registrations
controller. That is for registrations -- you should be using your users
controller:
#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
这只是控制器;您需要查看表单
正在呈现的方式通过。
This is just the controller; you need to look at how the form
is being rendered & passed.
如果您使用上述代码发送数据,则需要拉取表单
HTML并显示如何 Wicked
正在使用它。
If you're sending data with the above code, you'll want to pull your form
HTML and show how Wicked
is working with it.
您需要在 app / views / users / show.html.erb
中查看要显示的表单,不 / app / views / registrationments
You need to be looking in app/views/users/show.html.erb
for the form to display, not /app/views/registrations
这篇关于邪恶宝石与茧宝石和设计用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!