ActiveRecord::AssociationTypeMismatch when trying to register user using devise

扶醉桌前 提交于 2019-12-11 18:22:37

问题


I am trying to attach nested fields in register process with devise gem. I want to create video that belongs to User after User creation in one request.

Form sends post to create method from registrations_controller.rb and it fails at sign_up params

new.html.erb

 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), remote: true) do |f| %>
<input name="authenticity_token" 
               type="hidden" 
               value="<%= form_authenticity_token %>"/>   
<div data-behavior="devise-error-messages">
  <%= devise_error_messages! %>
</div>


  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

<div class="field">
<label for="user_title">title</label><br>
<input autofocus="autofocus" value="" name="user[videos][title]" id="user_desc" type="text">
</div>
<div class="field">
<label for="user_desc">desc</label><br>
<input autofocus="autofocus" value="" name="user[videos][desc]" id="user_title" type="text">
</div>
<% end %>

registrations_controller.rb

def create
    super
end

private
def sign_up_params
  params.require(:user).permit(:email, :password,
     password_confirmation, videos: [:title, desc])
end

User model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :videos,  inverse_of: :user
  accepts_nested_attributes_for :videos
end

Video model

class Video < ApplicationRecord
    belongs_to :user
end

console

Started POST "/users" for 127.0.0.1 at 2018-06-14 17:36:35 +0200
Processing by Users::RegistrationsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"XhRMYudWgjw4LMm1PzqOtRzy+MfdYotwBg6pZihGkJFTuBTDFd1iaXhXGmVNAoyQrCKOy9Nhy4gW7zJgGmXGvA==", "user"=>{"email"=>"abc@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "videos"=>{"title"=>"", "desc"=>""}}}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)



ActiveRecord::AssociationTypeMismatch (Video(#70239190589100) expected, got ["title", ""] which is an instance of Array(#19511580)):

app/controllers/users/registrations_controller.rb:16:in `create'

UPDATE 1

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create], if: :devise_controller?


protected   
def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :password, :password_confirmation,  videos: [:title, :desc]) }
end

end

problem persists

来源:https://stackoverflow.com/questions/50862218/activerecordassociationtypemismatch-when-trying-to-register-user-using-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!