gpt4 book ai didi

ruby-on-rails - 将 form_for 与复数类名一起使用时,rails 4 未定义方法 `_index_path'

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

对于这个错误,我在 SO 上浏览了很多问题,但不幸的是我没有找到正确的答案。

我已经使用 rails generate scaffold Avis 创建了 Avis 类(在法语中,它采用 s,无论它是单数还是复数。) --force-plural.

因为它是 Formation 类的一部分,这里是 route.rb 文件(部分):

resources :formations do
resources :avis
end

这是 Avis Controller :

class AvisController < ApplicationController
before_action :set_avi, only: [:show, :edit, :update, :destroy]
before_action :set_formation

#On indique que les tests sont fait sur l'autorisation des utilisateurs
load_and_authorize_resource :formation

# gestion du layout
layout :sections_layout
@layout = 'back'

respond_to :html

def sections_layout
@layout
end

def index
@avis = Avis.where(:formations_id => Formation.find_by(:id => formation_params))
respond_with(@avis)
end

def show
respond_with(@avi)
end

def new
@avi = Avis.new
respond_with(@formation, @avi)
end

def edit
end

def create
@avi = Avis.new(avis_params)
@avi.save
respond_with(@avi)
end

def update
@avi.update(avis_params)
respond_with(@avi)
end

def destroy
@avi.destroy
respond_with(@avi)
end

private
def set_avi
@avi = Avis.find(params[:id])
end

def avis_params
params[:avi]
end

def formation_params
params.require(:formation_id)
end

def set_formation
@formation = Formation.find_by(:id => params[:formation_id])
if @formation == nil
redirect_to forbidden_path :status => 403
end
end
end

当我尝试创建一个新的 Avis 时,我得到了这个错误:

ActionView::Template::Error (undefined method `avis_index_path' for #<#<Class:0x007ffa6052aa78>:0x007ffa60528b88>):
1: <% puts @avi%>
2:
3: <%= form_for(@avi) do |f| %>
4: <% if @avi.errors.any? %>
5: <div id="error_explanation">
6: <h2><%= pluralize(@avi.errors.count, "error") %> prohibited this avi from being saved:</h2>
app/views/avis/_form.html.erb:3:in `_app_views_avis__form_html_erb__481767065103572634_70356711120220'
app/views/avis/new.html.erb:3:in `_app_views_avis_new_html_erb___1382100742020377330_70356626529020'
app/controllers/avis_controller.rb:29:in `new'

我该如何解决这个问题?

最佳答案

AviFormations 的嵌套资源,您的 form_for 应该看起来像这样 form_for(@formation, @avi),它将使用正确的 formation_avis_path。

有关嵌套资源的更多信息,请查看 Rails Routing from the Outside In

关于ruby-on-rails - 将 form_for 与复数类名一起使用时,rails 4 未定义方法 `_index_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306258/

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