gpt4 book ai didi

ruby-on-rails - rails 中未定义的方法 "_path"

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

我正在尝试创建一个新模型并为用户提供一个表单链接以填充模型对象。

我的模型(/models/paypal_order):

class PaypalOrder < ActiveRecord::Base
attr_accessor :card_number, :card_verification

end

我的 Controller (controllers/paypal_order_controller.rb):

class PaypalOrderController < ApplicationController

def new
@paypal_order = PaypalOrder.new
end

end

我的观点(views/paypal_order/new.html.erb):

<%form_for PaypalOrder.new do |f|%>  #also tried <%form_for @paypal_order do |f|%>
<%= f.error_messages %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :card_type %><br />
<%= f.select :card_type, [["Visa","visa"],["MasterCard", "master"],["Discover","discover"],["American Express","american_express"]] %>
</p>
<p>
<%= f.label :card_number %><br />
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
<%= f.text_field :card_verification %>
</p>
<p>
<%= f.label :card_expires_on %><br />
<%= f.text_field :card_expires_on, :discard_day =>true, :start_year => Date.today.year, :end_year => (Date.today.year+25), :add_month_numbers => true %>
</p>
<%end%>

链接:

  <div class="payment_options">
<div class="available_payment_options">
<div class="online_payment_option">
<h3>PayPal</h3>
<%= link_to "Checkout", new_paypal_order_path %>
</div>

routes.rb 中的条目:

resources :paypal_order

但是在点击链接时出现以下错误:

NoMethodError in Paypal_order#new

Showing /home/nish/repos/new/test/voylla_staging_changes/app/views/paypal_order/new.html.erb where line #1 raised:

undefined method `paypal_orders_path' for #<#<Class:0x101f7e98>:0x101e5b94>
Extracted source (around line #1):

1: <%form_for PaypalOrder.new do |f|%>
2: <%= f.error_messages %>
3: <p>
4: <%= f.label :first_name %><br />

我无法理解为什么会出现此错误。还有为什么表单要查找 paypal_order**s**_path,不应该是 paypal_order_path

编辑 rake 路由输出:

                               paypal_order_index GET    /paypal_order(.:format)                                                   {:action=>"index", :controller=>"paypal_order"}
POST /paypal_order(.:format) {:action=>"create", :controller=>"paypal_order"}
new_paypal_order GET /paypal_order/new(.:format) {:action=>"new", :controller=>"paypal_order"}
edit_paypal_order GET /paypal_order/:id/edit(.:format) {:action=>"edit", :controller=>"paypal_order"}
paypal_order GET /paypal_order/:id(.:format) {:action=>"show", :controller=>"paypal_order"}
PUT /paypal_order/:id(.:format) {:action=>"update", :controller=>"paypal_order"}
DELETE /paypal_order/:id(.:format) {:action=>"destroy", :controller=>"paypal_order"}

最佳答案

首先将第一行改为:

<%= form_for @paypal_order do |f|%>

第二个改变路线到:

resources :paypal_orders

同时将 Controller 类更改为

class PaypalOrdersController < ApplicationController

..
end

并将 Controller 文件名改为paypal_orders_controller.rb

关于ruby-on-rails - rails 中未定义的方法 "_path",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830114/

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