You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中下拉列表报错undefined method `current_acounts_path'如何解决?

问题描述

我来自智利,正在开发一个预订引擎,需要在其中定义客户的往来账户(Current Account)。我制作了一个列表并放入select组件中,但出现了以下错误:

undefined method `current_acounts_path' for #ActionView::Base:0x00000000024478
Did you mean? current_accounts_path

请问该如何解决这个问题?以下是相关代码:

Controller

class CurrentAccountsController < ApplicationController
  before_action :authenticate_user!
  
  def index
    @currentaccounts = CurrentAcount.all
  end

  def new
    @currentaccount = CurrentAcount.new
    @clients = Client.all
  end

  def create
    @currentacount = CurrentAcount.new(set_params)
    @currentacount.client_id = @client.id
    if (@currentacount.save)
      redirect_to current_accounts_path, notice: "Cuenta Corriente Guardada" 
    else
      render :new
      flash.alert = "No se pudo Guardar Cuenta Corriente"
    end
  end

  def edit

  end

  def update
    @currentaccount.client_id =  @client.id
    if (@currentaccount.update(set_params))
      redirect_to current_accounts_path, notice: "Modificacion de Cuenta Corriente Guardada" 
    else
      render :edit
      flash.alert = "No se pudo Modificar la Cuenta Corriente"
    end
  end

  def show

  end

  def destroy
    if (@currentaccount.destroy)
      redirect_to current_accounts_path, notice: "Eliminada Cuenta Corriente"
    else
      redirect_to current_accounts_path, notice: "No se pudo Eliminar Cuenta Corriente"
    end
  end

  private

  def set_params
    params.require(:current_acount).permit(:mail, :client_id, :number, :bank, :city, :telephone_contact, :type_account)
  end

  def set_client
    @client = Client.find(params[:client_id])
  end

end

Model

class CurrentAcount < ApplicationRecord
  belongs_to :client
end

Form

<%= simple_form_for @currentaccount do |f| %>
  <%= f.association :client, collection: @clients.map { |c| [c.name, c.id] } %>
  <%= f.input :number, label: "Numero cuenta Corriente" %>
  <%= f.input :type_account, prompt:"Elija tipo de cuenta", collection: ["Cuenta Correinte", "Cuenta Vista", "Cuenta de Ahorro"]%>
  <%= f.input :bank, prompt:"Elija un Banco", collction: ["Banco de Chile", "Banco Internacional", "Scotiabank Chile ", "BCI", "Corpbanca", "Bnaco BICE", "HSBC Bank (Chile)", "Banco Santander-Chile", "Banco Itaú Chile", "Banco Security", "Banco Falabella", "Deutche Bank (Chile)", "Banco Ripley", "Rabobank Chile", "Banco Consorcio", "Banco Penta", "Banco Paris", "Banco Bilbao Vizcaya Argentaria, Chile (BBVA)", "Banco BTG Pactual Chile", "BancoEstado", "Banco Do Brasil S.A.", "JP MORGAN BANK, N.A.", "Banco de La Nacion Argentina", "The Bank of Tokyo-Mitsubishi UFJ, LTD"] %>
  <%= f.input :city, label: "Ciudad"%>  
  <%= f.input :telephone_contact, label: "Telefono"%>
  <%= f.input :email_contact, label: "Email"%>
  <%= f.submit "Agregar" %>
  <%= link_to  "Volver", current_accounts_path(@reservation.client_id) %>
<% end %>

解决方案

核心问题:拼写错误

错误提示已经给出明确线索——Did you mean? current_accounts_path,说明代码里把Account拼成了Acount(少一个c),导致Rails无法匹配对应的路由助手方法。

分步修复

  1. 修正模型名称

    • 将模型文件名从app/models/current_acount.rb改为app/models/current_account.rb
    • 模型类名同步修正:
      class CurrentAccount < ApplicationRecord
        belongs_to :client
      end
      
  2. 修正控制器中的模型调用
    控制器内所有CurrentAcount替换为CurrentAccount,同时建议实例变量遵循Rails下划线命名规范(比如@currentaccount改为@current_account):

    def index
      @current_accounts = CurrentAccount.all
    end
    
    def new
      @current_account = CurrentAccount.new
      @clients = Client.all
    end
    
    def create
      @current_account = CurrentAccount.new(set_params)
      # 后续代码同步修正变量名
    end
    
  3. 修正路由配置
    确保config/routes.rb中的资源路由拼写正确:

    resources :current_accounts
    
  4. 修复其他代码问题

    • 控制器create和update方法调用了@client.id,但未触发set_client前置方法,需添加:
      before_action :set_client, only: [:create, :update]
      
    • 表单中bank字段的collction拼写错误,改为collection
    • 表单里的email_contact字段未加入控制器参数白名单,修正set_params:
      def set_params
        params.require(:current_account).permit(:email_contact, :client_id, :number, :bank, :city, :telephone_contact, :type_account)
      end
      
    • edit、update、show、destroy方法缺少实例加载逻辑,添加前置方法:
      before_action :set_current_account, only: [:edit, :update, :show, :destroy]
      
      private
      
      def set_current_account
        @current_account = CurrentAccount.find(params[:id])
      end
      

内容的提问来源于stack exchange,提问作者Juan Pablo Grover Pinto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 15:45:29