gpt4 book ai didi

ruby-on-rails - 注册时以嵌套形式传递 user_id 并保存两个模型

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:06 24 4
gpt4 key购买 nike

我的 Devise 注册表单上有一个嵌套表单,它填充了一个连接表。连接表信息未保存,因为 user_id 为 nil。这是因为他们还没有登录,所以我不能通过 current_user。如何通过 user_id 以一种形式保存连接表(语言用户)?

new.html.erb 的相关部分

 <%= form_for(resource, as: resource, url: registration_path(resource)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<%= f.fields_for :languages_users do |lu| %>
<div class="fields">
<%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
<%= lu.hidden_field :level, value: 1 %>
<%#= lu.hidden_field :user_id, value: current_user.id %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>

registrations_controller.rb

    class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def new
build_resource({})
resource.languages_users.build(user_id: current_user)
respond_with self.resource
end
def create
super
end
private
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [ :language_id, :user_id, :level]) }
end
end

这是我的参数:

  {"utf8"=>"✓",
"authenticity_token"=>"v/uM0+B7/XCUKmhyFGoEKqunCllXAK50uZN5f+TDpUsxlnIsgQVPbXhEb1q1P5pmG586iaULRROyarWzAzz12w==",
"user"=>{"first_name"=>"Walsh",
"last_name"=>"Costigan",
"location"=>"w",
"nationality"=>"United States",
"bio"=>"w",
"email"=>"walsh@seamlessdocs.com",
"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fdd25023f78 @tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160403-6431-1hpthwr.JPG>,
@original_filename="IMG_4574.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4574.JPG\"\r\nContent-Type: image/jpeg\r\n">},
"languages_users_attributes"=>{"language_id"=>"18",
"level"=>"1"},
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}

更新** 目前这是我的代码所在的位置。它保存了用户,但不是 languages_user 模型

用户.rb 创建后:创建语言用户

def create_languages_user
@users = self.id
@languages_user = LanguagesUser.new(@user)
end

注册 Controller :

def new
@user = User.new
build_resource({})
respond_with self.resource
end

def create
super
end

private

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [ :language_id, :level]) }
end

最佳答案

很简单。只需将 block 传递给 Users::RegistrationsController Controller 中的 #create 方法

def create
super do |user|
language_user_params = params[:user][:langauges_user].permit(:language_id,:level)
user.languages_users.create(language_id: language_user_params[:language_id], level: language_user_params[:level])
end
end

关于ruby-on-rails - 注册时以嵌套形式传递 user_id 并保存两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com