我正在尝试创建一个新模型并为用户提供指向表单的链接以填充模型对象。
我的模型(/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
My view (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
吗
编辑前耙路线输出:
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
同时将控制器类更改为
class PaypalOrdersController < ApplicationController
..
end
和控制器文件名到 PayPal_orders_controller.rb
创建resources
控制器时,必须使用模型名称的复数形式。如果将控制器重命名为 PaypalOrdersController
并将路由更改为 resources :paypal_orders
它应该可以工作。