1

だから私はでUser生成されたモデルを持っています.gemdeviseを使用Wickedして、ユーザーからより多くのデータを取得してユーザーモデルに保存するためのマルチフォームオプションを提供しています。

すべてが正常に機能していますが、ユーザーが多くの学位を持つ別のモデルを追加しようとしていることを知っています。私は 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 (これは邪悪な 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

また、入力したフィールドを表示するにはどうすればよいですか。たとえば、ユーザー名を表示したい場合は次のようになります。

<%= @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'
4

1 に答える 1

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にレンダリングされて渡されるかを確認する必要があります。

上記のコードでデータを送信している場合は、formHTMLをプルWickedして、それがどのように機能するかを示したいと思うでしょう。

app/views/users/show.html.erb表示するフォームを探している必要があります /app/views/registrations

于 2016-02-09T17:30:01.423 に答える