gpt4 book ai didi

ruby-on-rails - Rails fields_for不起作用

转载 作者:行者123 更新时间:2023-12-02 04:43:59 25 4
gpt4 key购买 nike

我正在尝试使用fields_for并创建一个嵌套表单,但是只有一个文本字段显示为空白。我有3个crewmember记录。
crewmember模型:

class Crewmember < ActiveRecord::Base
belongs_to :production
belongs_to :callsheet

validates :firstname, presence: true
validates :email, presence: true

def name
"#{firstname} #{lastname}"
end

end
callsheet模型
class Callsheet < ActiveRecord::Base
attr_accessible :crewmembers_params
has_many :castmembers
has_many :crewmembers

accepts_nested_attributes_for :crewmembers
end
callsheets Controller
class CallsheetsController < ApplicationController
def index
@callsheets = Callsheet.all
@departments = Department.where(production_id: current_user.default_working_production_id)
end

def show
@callsheet = Callsheet.find(params[:id])
end

def new
@callsheet = Callsheet.new
@departments = Department.where(production_id: current_user.default_working_production_id)
end

def edit
@callsheet = Callsheet.find(params[:id])
end

def create
@callsheet = Callsheet.new(callsheets_params)
@Callsheet.production_id = current_user.default_working_production_id


if @callsheets.save
redirect_to callsheet_path
else
render 'new'
end
end

def update
@callsheet = Callsheet.find(params[:id])

if @callsheet.update(callsheets_params)
redirect_to callsheet_path, :notice => "callsheets successfully updated."
else
render 'edit', :notice => "callsheets not updated."
end
end

def destroy
@callsheet = Callsheet.find(params[:id])
@callsheet.destroy

redirect_to callsheets_path
end

private
def callsheets_params
params.require(:callsheet).permit(:crewmembers_params [:id, :firstname])

end
end

新 call 表的形式:
<%= form_for @callsheet do |f| %>
<% if @callsheet.errors.any? %>
<div id="error_explanation" class="alert alert-danger">
<strong>
<%= pluralize(@callsheet.errors.count, "error") %> prohibited
this call sheet from being saved:
</strong>
<ul>
<% @callsheet.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= f.fields_for :crewmember do |crewmember| %>
<fieldset>
<%= crewmember.label :firstname, "First Name" %><br />
<%= crewmember.text_field :firstname %>
</fieldset>
<% end %>

<% end %>

最佳答案

您不需要attr_accessible(仅适用于Rails 3)。

您还应该将所有模型重命名为 snake_case ,并引用 CamelCase :

#app/models/call_sheet.rb
class CallSheet < ActiveRecord::Base
has_many :cast_members
has_many :crew_members
accepts_nested_attributes_for :crew_members
end

fields_for的习惯一样,您还需要构建关联的对象(如果要创建 new记录)(如果是 editing则不需要这样做):
#app/controllers/call_sheets_controller.rb
class CallSheetsController < ApplicationController
before_action :set_departments

def new
@callsheet = Callsheet.new
@callsheet.crew_members.build
end

def edit
@callsheet = Callsheet.find params[:id]
end

def update
@callsheet = Callsheet.find params[:id]
@callsheet.update callsheet_params
end

private

def set_departments
@departments = Department.where(production_id: current_user.default_working_production_id)
end

def callsheet_params
params.require(:callsheet).permit(crew_members_attributes: [:id, :firstname])
end
end

这将允许您使用:
<%= form_for @callsheet do |f| %>
<%= f.fields_for :crew_members do |crewmember| %>
<%= crewmember.label :firstname, "First Name" %><br />
<%= crewmember.text_field :firstname %>
<% end %>
<%= f.submit %>
<% end %>

-

通过 fields_for传递嵌套属性时,您需要几个组件:
  • 父模型中正确的关联
  • 关联模型的实例化版本(@parent.build_child)
  • 正确的fields_for定义
  • 通过 Controller
  • 传递正确的 parameters
    我已经概述了如何实现上述目标,而这些都是您不正确的。

    您还可以在同一调用中声明多个验证:
    #app/models/crew_member.rb
    class CrewMember < ActiveRecord::Base
    validates :firstname, :email, presence: true
    end

    关于ruby-on-rails - Rails fields_for不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940338/

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