如果对此的回答很明显,请道歉,但我只有一个半月进入Rails。
基本上,我有一个编辑餐厅表格。
= simple_form_for(restaurant, html: { class: 'session_form with_padding' }) do |f|
%h3
%b
Manager Details
= simple_fields_for :promoter do |p|
.row
.col-xs-12.col-sm-12.col-md-6.col-lg-6
= p.label :first_name, required: false, class: 'control-label'
.form-group{:class => ('has-error' if restaurant.errors[:manager_first_name].first)}
= p.text_field :first_name, required: true, autofocus: true, class: 'form-control', value: restaurant.promoter.first_name
%h5
= p.full_error :first_name
.row
.col-xs-12.col-sm-12.col-md-6.col-lg-6
= p.label :last_name, required: false, class: 'control-label'
.form-group{:class => ('has-error' if restaurant.errors[:manager_last_name].first)}
= p.text_field :last_name, required: true, autofocus: true, class: 'form-control', value: restaurant.promoter.last_name
%h5
= p.full_error :last_name
%h3
%b
Restaurant Details
.row
.col-xs-12.col-sm-12.col-md-6.col-lg-6
= f.label :name, required: false, class: 'control-label'
.form-group{:class => ('has-error' if restaurant.errors[:name].first)}
= f.text_field :name, required: true, autofocus: true, class: 'form-control'
%h5
= f.full_error :name
= f.label :address, required: false, class: 'control-label'
.form-group{:class => ('has-error' if restaurant.errors[:address].first)}
%input#pac-input.controls{:placeholder => "Search address", :type => "text", class: 'form-control'}
%h5
= f.full_error :address
在表单内部,我有一个“管理器详细信息”的嵌套表单。我在这里想要实现的是在更新细节时保存细节。我编辑了“餐厅详细信息”和“经理详细信息”字段,然后点击了提交按钮,但在终端中,我只能看到餐馆中某列的SQL更新,但没有更新到启动器列。
另外,我有两个型号,促销员和餐厅。促销员has_many餐厅和餐厅belongs_to发起人。
class Restaurant < ActiveRecord::Base
...
belongs_to :promoter
accepts_nested_attributes_for :promoter
...
end
class Promoter < ActiveRecord::Base
has_many :restaurants
...
end
这是餐厅的控制员。
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:edit, :update, :destroy]
before_action :authenticate_promoter!
def create
@restaurant = Restaurant.new(restaurant_params)
@restaurant.update(promoter_id: current_promoter.id)
if @restaurant.save
return render :crop if logo_exists?
redirect_to restaurant_path(@restaurant)
flash[:notice] = 'Restaurant was successfully created.'
else
render :new
end
end
def edit
end
def update
if @restaurant.update(restaurant_params)
return render :crop if logo_exists?
redirect_to @restaurant, notice: 'Restaurant was successfully updated.'
else
render :edit
end
end
private
def set_restaurant
@restaurant = Restaurant.find(params[:id])
end
def restaurant_params
return permitted_params unless permitted_params[:phone_number].present?
parameters = permitted_params
parameters[:phone_number] =
"+#{params[:country_code]}#{parameters[:phone_number]}"
parameters
end
def permitted_params
params
.require(:restaurant)
.permit(:name, :address, :latitude, :longitude, :phone_number,
:manager_first_name, :website, :manager_last_name,
:manager_email, :manager_phone_number, :logo, :logo_cache,
:crop_x, :crop_y, :crop_w, :crop_h, :service_charge, :gst,
:first_name, :last_name, :email,
operating_hours_attributes: [:id, :day, :opening_time,
:closing_time, :enabled, :_destroy])
end
end
促销员的控制者
class Promoters::RegistrationsController < Devise::RegistrationsController
after_action :subscribe_mailchimp, only: :create, if: '@promoter.persisted?'
def create
@promoter = Promoter.new(promoter_params)
if @promoter.save
sign_in(resource_name, resource)
redirect_to restaurants_path, notice:
'Welcome! You have signed up successfully'
else
render :new
end
end
private
def promoter_params
parameters = permitted_params
parameters[:phone_number] =
"+#{params[:country_code]}#{parameters[:phone_number]}"
parameters
end
def permitted_params
params
.require(:promoter)
.permit(
:first_name, :last_name, :email, :phone_number,
:password, :password_confirmation, :country_code,
:terms)
end
end
这里粘贴的代码是我认为必要的代码。如果它太长了,那么。
我的问题是,为什么fields_for:promotion不更新数据库中的数据?我会尽可能地添加我的路线文件。
答案 0 :(得分:1)
我认为您的错误就在这一行
= simple_fields_for :promoter do |p|
替换为:
= f.fields_for :promoter do |p|
答案 1 :(得分:0)
您需要让simple_fields_for
的接收者将启动器字段与较大的表单相关联。
而不是
= simple_fields_for :promoter do |p|
待办事项
= f.simple_fields_for :promoter do |p|