我见过与这个类似问题的其他联系,但似乎没有一个在我的情况下有效。
我的更新操作是在mysql数据库中创建新记录。
我的应用程序由3种型号组成
FashionModelModelProfile和Measurement它们的定义如下:
class FashionModel < ActiveRecord::Base
has_secure_password
has_one :model_profile
has_one :measurement
accepts_nested_attributes_for :model_profile
accepts_nested_attributes_for :measurement
end
class ModelProfile < ActiveRecord::Base
belongs_to :fashion_model
end
class Measurement < ActiveRecord::Base
belongs_to :fashion_model
endfashion_model_controller.rb如下:
class FashionModelsController < ApplicationController
def new
@fashion_model = FashionModel.new
end
def create
@fashion_model = FashionModel.new(fashion_models_sign_up_params)
if @fashion_model.save
flash[:success] = "Welcome to Meriad"
session[:fashion_model_id] = @fashion_model.id
redirect_to edit_fashion_model_path(@fashion_model)
else
render 'new'
end
end
def edit
@fashion_model = FashionModel.find(params[:id])
@fashion_model.build_model_profile
@fashion_model.build_measurement
end
def update
@fashion_model = FashionModel.find(params[:id])
if @fashion_model.update(fashion_models_edit_params)
flash[:success] = "Saved!"
redirect_to fashion_model_path(@fashion_model)
else
render 'edit'
end
end
def show
@fashion_model = FashionModel.find(params[:id])
end
endfashion_model_edit_params是
def fashion_models_edit_params
params.require(:fashion_model).permit(:first_name,
:last_name,
:email,
:password,
model_profile_attributes: [:id,
:location,
:bio, :gender,
:phone_number,
:rate,
:profile_image,
:birthdate],
measurement_attributes: [:id,
:feet,
:inches,
:bust,
:waist,
:hips,
:dress,
:shoes,
:hair,
:eyes])
end我想在这些台词上写点什么:
new.html.erb (存储在fashion_models表中)注册这个应用程序。index.html.erb包含所有fashion_models的列表,并有一个编辑信息的选项(更新它们的配置文件)。edit.html.erb包含来自model_profiles表和measurements表的字段,这两个表都具有fashion_models表的外键。我的fashion_models/new.html.erb模板非常简单,包含了first_name、last_name、email、password。
我的fashion_models/edit.html.erb模板类似于:
<%= form_for @fashion_model do |f| %>
# fashion_model fields here
<%= f.fields_for :model_profile do |t| %>
# model_profile fields here
<% end %>
<%= f.fields_for :measurement do |t| %>
# measurement fields here
<% end %>
<% end %>现在,每当我编辑fashion_model/:id时,model_profile和measurement都会在数据库中创建一个新记录,而不是更新现有的记录。而且,当我在Edit Profile页面上时,所有字段都不会预先填充现有数据。我必须再次手动输入所有数据。
首先,我认为这是因为build方法,但是当我删除它们时,fields_for就不会显示。
感谢你的帮助!
发布于 2016-06-20 14:34:06
所以我终于想出了办法。希望它能帮到别人。
我的问题是,我在一个fields_for中有2个form_for,我需要用这些表单更新2个模型。
我厌倦了使用像after_action :create_model_profile和after_action :create_measurement这样的回调。在这两种方法中,我都试图将fashion_model_id保存为新创建的fashion_model的:id。
但是由于回调被认为是代码闻起来是,所以我想要一个更好的解决方案。
我没有创建一个form_for @fashion_models,而是编写了一个助手方法,如下所示
# app/helpers/form_helper.rb
module FormHelper
def setup_fashion_model(fashion_model)
fashion_model.model_profile ||= ModelProfile.new
fashion_model.measurement ||= Measurement.new
fashion_model
end
end在我的form_for里我用了
form_for(setup_fashion_model(@fashion_model)) do |f|
...
end这创建了一个新的ModelProfile和Measurement实例,如果它没有在数据库中持久存在的话。
当然,它解决了预先填充包含数据的字段的问题。
就是这样。简单的解决办法,但我一直在那里挠我的头。希望能帮上忙。
发布于 2016-06-18 23:37:35
你可能把他们的路线搞得一团糟。
它的工作方式如下:
form_for调用@fashion_model.new_record?POST /fashion_model,并创建一个对象。PUT /fashion_model/:id,并更新一个对象。正如您所看到的,它仅仅取决于该对象是否已经存在于数据库中。因此,再次检查使用form_for的位置以及传递它的对象。
有关路线的问题,请参见:http://guides.rubyonrails.org/routing.html
https://stackoverflow.com/questions/37901348
复制相似问题