form_with 出现错误(未定义的方法)
Getting error (undefined method) for form_with
我有_form.html.erb。在代码的第一行,它抛出了这个错误:
#ActionView::Base:0x00000000229750 的未定义方法“assessments_path”
你的意思? asset_path
几点:
- 在路由中 'poweruser' 是一个命名空间
- 在模型中 assessment.rb 是
不是文件夹 poweruser(因为我计划将它用于任何人都可以访问的其他页面,而不仅仅是 powerusers)。
- 知道我遗漏了什么吗?
代码如下:
<%= form_with(model: [@assessment], local: true) do |form| %>
<%= render "shared/error_messages", resource: form.object %>
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>
<% end %>
这是控制器
module Powerusers
class AssessmentsController < ApplicationController
before_action :authenticate_user!
# before_action :set_assessment
# before_action :set_assessment, only: [:set_assessment, :show, :edit, :update, :destroy]
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end
def index
@pagy, @assessments = pagy(Assessment.sort_by_params(params[:sort], sort_direction))
# We explicitly load the records to avoid triggering multiple DB calls in the views when checking if records exist and iterating over them.
# Calling @assessments.any? in the view will use the loaded records to check existence instead of making an extra DB call.
@assessments.load
end
# GET /assessments/new
def new
@assessment = Assessment.new
@assessment.assessment_sections.new
end
这里是 assessment.rb(不是 不是 在子文件夹 poweruser 中)
class Assessment < ApplicationRecord
belongs_to :company
has_many :assessment_sections, inverse_of: :assessment
has_many :questions, through: :assessment_sections
accepts_nested_attributes_for :assessment_sections, reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :questions, reject_if: :all_blank,
allow_destroy: true
end
这是路线
# PowerUser
authenticated :user, lambda { |u| u.admin? } do
namespace :powerusers do
resources :assessments do
resources :assessment_sections do
resources :questions
end
end
end
end
由于路由中有命名空间,因此需要使用 form_with
指定
<%= form_with(model: [:powerusers, @assessment], local: true) do |form| %>
我有_form.html.erb。在代码的第一行,它抛出了这个错误:
#ActionView::Base:0x00000000229750 的未定义方法“assessments_path” 你的意思? asset_path
几点:
- 在路由中 'poweruser' 是一个命名空间
- 在模型中 assessment.rb 是 不是文件夹 poweruser(因为我计划将它用于任何人都可以访问的其他页面,而不仅仅是 powerusers)。
- 知道我遗漏了什么吗?
代码如下:
<%= form_with(model: [@assessment], local: true) do |form| %>
<%= render "shared/error_messages", resource: form.object %>
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>
<% end %>
这是控制器
module Powerusers
class AssessmentsController < ApplicationController
before_action :authenticate_user!
# before_action :set_assessment
# before_action :set_assessment, only: [:set_assessment, :show, :edit, :update, :destroy]
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end
def index
@pagy, @assessments = pagy(Assessment.sort_by_params(params[:sort], sort_direction))
# We explicitly load the records to avoid triggering multiple DB calls in the views when checking if records exist and iterating over them.
# Calling @assessments.any? in the view will use the loaded records to check existence instead of making an extra DB call.
@assessments.load
end
# GET /assessments/new
def new
@assessment = Assessment.new
@assessment.assessment_sections.new
end
这里是 assessment.rb(不是 不是 在子文件夹 poweruser 中)
class Assessment < ApplicationRecord
belongs_to :company
has_many :assessment_sections, inverse_of: :assessment
has_many :questions, through: :assessment_sections
accepts_nested_attributes_for :assessment_sections, reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :questions, reject_if: :all_blank,
allow_destroy: true
end
这是路线
# PowerUser
authenticated :user, lambda { |u| u.admin? } do
namespace :powerusers do
resources :assessments do
resources :assessment_sections do
resources :questions
end
end
end
end
由于路由中有命名空间,因此需要使用 form_with
<%= form_with(model: [:powerusers, @assessment], local: true) do |form| %>