未定义的方法" _path"在铁轨

时间:2014-04-03 06:59:59

标签: ruby-on-rails ruby ruby-on-rails-3

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

我的模特(/ models / paypal_order):

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

end

我的控制器(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"}

2 个答案:

答案 0 :(得分:3)

首先将第一行更改为:

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

第二次改变路线:

resources :paypal_orders

还将控制器类更改为

class PaypalOrdersController < ApplicationController

..
end

将控制器文件名改为paypal_orders_controller.rb

答案 1 :(得分:2)

创建resources控制器时,必须使用复数型号名称。如果您将控制器重命名为PaypalOrdersController并将路由更改为resources :paypal_orders,则应该可以正常运行。