Ruby on rails 在Rails中使用一个表单更新多个模型
我是Rails新手,尝试构建一个允许用户填写和提交表单的表单。表单完成了他们的配置文件。我希望表单能够更新Ruby on rails 在Rails中使用一个表单更新多个模型,ruby-on-rails,model-view-controller,associations,fields-for,Ruby On Rails,Model View Controller,Associations,Fields For,我是Rails新手,尝试构建一个允许用户填写和提交表单的表单。表单完成了他们的配置文件。我希望表单能够更新用户模型(和数据库表)以及兴趣模型(嗜好)。我正在使用一个带有字段的表单,用于 但是,提交表单不会更新兴趣模型。它要么只更新用户模型,要么返回各种错误(以下生成的兴趣用户必须存在错误): 型号: class User < ApplicationRecord has_many :interests, foreign_key: "interests_user_id&quo
用户
模型(和数据库表)以及兴趣
模型(嗜好)。我正在使用一个带有字段的表单,用于
但是,提交表单不会更新兴趣
模型。它要么只更新用户
模型,要么返回各种错误(以下生成的兴趣用户必须存在
错误):
型号:
class User < ApplicationRecord
has_many :interests, foreign_key: "interests_user_id"
accepts_nested_attributes_for :interests
end
class Interest < ApplicationRecord
belongs_to :user
self.primary_key = "interest_id"
end
也许这个示例可以帮助您小心地使用params.require(:user).permit(user.attribute_names.map(&:to_sym),
在允许的参数中。这感觉像是一个很好的射中你自己脚的方式。我真的很想知道为什么你在兴趣表中的列前加前缀会让事情变得更加困难。除非你真的有理由,否则就坚持惯例。我不明白你的意思,max。“在列前加前缀。”我正在尝试了解约定是什么。到目前为止,我的解决方案是将可选:true
添加到兴趣模型中。兴趣
表上的所有列都带有前缀-兴趣用户id
,兴趣id
。这是多余的,而且非常不一致。要么全部加前缀,要么不加前缀。H只保留一个带有前缀的表是令人困惑的。在Rails中,惯例是前缀是愚蠢的,我们不这样做。
class UsersController < ApplicationController
def index
@allUsers = User.all
end
def new
@user = User.new
@user.interests.build
end
def create
@user = User.new permitted_params
=begin
@user = User.new(
:author => params[:user][:author],
:user_name => params[:user][:user_name],
:user_bio => params[:user][:user_bio],
)
@user.interests.new(
:interest_name => params[:user][:interests_attributes][:interest_name],
:interest_experience_level => params[:user][:interests_attributes][:interest_experience_level],
:interest_positon => params[:user][:interests_attributes][:interest_positon],
)
=end
if @user.save
redirect_to users_url
else
render 'new'
end
end
private
def permitted_params
#params.require(:interest).permit(User.attribute_names.map(&:to_sym),interest.attribute_names.map(&:to_sym))
params.require(:user).permit(User.attribute_names.map(&:to_sym), interests_attributes: [:interest_name,:interest_experience_level,:interest_positon])
end
end
<%= form_with model: @user do |newUser| %>
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li> <%= message %></li>
<%end%>
</ul>
<%end%>
<div class="form-sections" id="user-profile">
<h3>General Information</h3>
<table class="formTables" id="formMainInfo">
<%= newUser.hidden_field :author, value:1 %>
<tr class="formRows">
<td class="formDescriptionCol"><%= newUser.label :user_name, "Your Name" %></td>
<td class="formInputCol">
<%= newUser.text_field :user_name %>
</td>
</tr>
<tr class="formRows">
<td class="formDescriptionCol"> <%= newUser.label :user_bio, "Biography" %></td>
<td class="formInputCol">
<%= newUser.text_area :user_bio %>
</td>
</tr>
<h3>Interests</h3>
<%= newUser.fields_for :interests do |newInterest| %>
<tr class="interestRows">
<td >Interest Name:</td>
<td>
<%= newInterest.text_field :interest_name%>
</td>
</tr>
<tr class="interestRows">
<td ><%=newInterest.label :interest_experience_level, "Experience Level" %></td>
<td >
<%= newInterest.text_field :interest_experience_level %>
</td>
</tr>
<tr class="interestRows">
<td ><%= newInterest.label :interest_position, "Position" %></td>
<td >
<%= newInterest.text_field :interest_position %>
</td>
</tr>
<%end%>
</table>
</div>
<div class="formSections" id="buttonsSection">
<%= newUser.submit "submit" %>
</div>
<%end%>
create_table "interests", primary_key: "interest_id", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "interest_user_id", null: false
t.string "interest_name", limit: 45, null: false
t.string "interest_experience_level", limit: 45, null: false
t.string "interest_position", limit: 45, null: false
t.index ["interest_user_id"], name: "user_id_idx"
end
create_table "users", primary_key: "user_id", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "user_name", limit: 60, null: false
t.string "author", limit: 60, null: false
t.string "user_bio", limit: 500, null: false
end
add_foreign_key "interests", "users", column: "interest_user_id", primary_key: "user_id", name: "interest_user_name", on_update: :cascade, on_delete: :cascade