gpt4 book ai didi

ruby-on-rails - 按照 Rails 教程,未定义的方法 'create' 用于 nil :NilClass, 和嵌套资源

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

我正在遵循 Rails 教程并在适当的地方进行更改,目的是在教程完成后我的教程项目将成为一个成熟的生产应用程序。

我在教程的第二个模型部分遇到了问题。这是我编写第二个模型的方式。

在我的 policy.rb 中:

class Policy < ApplicationRecord
has_one :insured
end

在我的 insured.rb 中:

class Insured < ApplicationRecord
belongs_to :policy
end

在我的 routes.rb 中:

resources :policies do
resource :insured
end

在我的 insureds_controller.rb 中:

def create
@policy = Policy.find(params[:policy_id])
# next line is raising the error
@insured = @policy.insured.create(insured_params)
redirect_to @insured
end

private
def insured_params
params.permit(:name, :address, :phone, :email)
end

我已经使用 render plain: @policy.inspect 检查了 @policy 对象,并且可以确认 ActiveRecord 正在正确检索策略。当我检查 @policy 的属性时,使用 render plain: @policy.attribute_names.inspect,我没有看到 insured 属性,我认为 Rails 应该自动为我管理。在本教程中,一个article has_many :comments,并且评论很容易创建并与父文章相关联,调用如下:@article.comments.create (评论参数)。我还注意到教程使用了 params.require(:comment).permit(...) 而我必须使用 params.permit(...),之后检查 params 哈希,我看到 :insured 属性存在于哈希的顶层,而不是绑定(bind)到 :insured哈希中的键。

我尝试像这样手动保存和分配 @insured 对象:

def create
@policy = Policy.find(params[:policy_id])
@insured = Insured.new(insured_params)
if @insured.save
@policy.insured = @insured
redirect_to @insured
end
end

只是在我的 .../insureds/new.html.erb 中遇到以下错误:

<h1>New Insured</h1>
<h1><%= @policy.policy_number %></h2>
<%= render 'form' %>
<%= link_to 'Cancel', policy_path(@policy) %>

源 self 的部分形式 .../insureds/_form.html.erb:

# the following line raises the error
<%= form_with model: @insured, local: true do |form| %>
# html omitted for brevity
<% end %>

错误:“未定义的方法 insureds_path”。这很奇怪,因为当我检查 HTML 时,我可以看到此 View 的表单操作是 /policies/[:id]/insured

对于大量的文字墙,我很抱歉,我想向你们展示我确实试图找出问题所在。

最佳答案

您的config/routes.rb 文件中有一个错误:

resources :policies do
# change it for:
collection do
get 'insured', to: 'policies#show_insured', as: 'show_policy_insured'
# maybe unnecessary to be here
# get 'insured/new', to: 'insureds#new', as: 'new_policy_insured'
# post 'insured/create', to: 'insureds#create', as: 'create_policy_insured'
# delete 'insured/delete', to: 'insureds#delete', as: 'delete_policy_insured'
end
end
# add resources here
resources :insureds

policy_controller.rb 中:

def show_insured # 'policy/:id/insureds/
end

insureds_controller.rb 中:

def show # '/insureds/:id'
end

def create
...
redirect_to show_policy_insured && return if @insured_policy
end

# before_filter or before_action
@policy = Policy.find(params[:id])
@insured_policy = @policy.insured

检查并运行以查看您的路线:

$ bundle exec rake routes 


get /policies/:id/insured => 'policies_controller#show_insured'
get /insureds/:id => 'insureds_controller#show'
get /insured/new => 'insureds_controller#new'
post /insureds/create => 'insureds_controller#create'
delete /insureds/:id/delete => 'insureds_controller#delete'

关于ruby-on-rails - 按照 Rails 教程,未定义的方法 'create' 用于 nil :NilClass, 和嵌套资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49388378/

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