gpt4 book ai didi

ruby-on-rails - 使用单表继承处理表单

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

我的个人资料编辑 View 中有一个以这一行开头的表单:

<% form_for @profile, :html => { :multipart => true } do |f| %>

Profile进行单表继承,两个子类Profile::Artist和Profile::Listener。

当我尝试访问配置文件的编辑 View 时,出现此错误:

NoMethodError in Profiles#edit

Showing /rubyprograms/dreamstill/app/views/profiles/edit.html.erb where line #1 raised:

undefined method `profile_artist_path' for #<#<Class:0x103359a18>:0x1033560c0>

第 1 行是我在上面发布的表单的代码行。我该如何解决这个错误?

更新:

我将此代码添加到我的个人资料模型中:

def self.inherited(child)
child.instance_eval do
def model_name
Vehicle.model_name
end
end
super
end

现在我的错误变成了:

NameError in Profiles#edit

Showing /rubyprograms/dreamstill/app/views/profiles/edit.html.erb where line #1 raised:

uninitialized constant Profile::Vehicle

更新 2:

我把表格的第一行改成了:

<% form_for(:profile, @profile, :url => {:controller => "profiles", :action => "update"}, :html => { :multipart => true }) do |f| %>

和提交按钮到<%= f.submit :profile %>

现在我只收到一个路由错误...

最佳答案

不是Vehile而是Profile!

def self.inherited(child)
child.instance_eval do
def model_name
Profile.model_name
end
end
super
end

def self.model_name
name = "profile"
name.instance_eval do
def plural; pluralize; end
def singular; singularize; end
def i18n_key; singularize; end
def human(*args); singularize; end
end
return name
end

更新

一个实际的问题是形式。您应该添加 :method => :put

<%= form_for(@profile, :html => { :multipart => true, :method => :put }) do |f| %>

关于ruby-on-rails - 使用单表继承处理表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5455540/

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