Wicked Gem with cocoon gem and devise user model

2019-07-26 23:39发布

问题:

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.

user model:

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

degree model:

belongs_to :user

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

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

Im getting an error in the user_steps_controller:

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

and that the error is within this line:

 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| %>

Terminal log:

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:

This is too long to comment, I fully expect to update it.


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"}

This might be Wicked, but judging form your spurious use of RegistrationsController, I'd imagine it could be to do with formatting etc:

--

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.

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.

You need to be looking in app/views/users/show.html.erb for the form to display, not /app/views/registrations