【发布时间】:2013-09-08 23:22:29
【问题描述】:
我有以下型号:
class Coupon < ActiveRecord::Base
belongs_to :company
validates :description, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :fine_print, presence: true
end
以及优惠券控制器中的以下方法:
def redeem
if params[:pin] == @coupon.company.pin
redirect_to root_path
else
flash.now[:notice] = "Incorrect Pin"
render :show
end
end
这个表单在a视图中:
<%= form_for( @coupon, :url => coupons_redeem_path( @coupon ), :html => { :method => :post } ) do |f| %>
<%= label_tag("pin", "Search for:") %>
<%= text_field_tag("pin") %>
<%= f.submit "Close Message" %>
<% end %>
我希望表单在点击提交时调用优惠券控制器中的兑换方法,但出现此错误:
没有路线匹配 [POST] "/coupons/redeem.1"
编辑
这些是我的路线:
resources :companies do
resources :coupons
end
get 'coupons/redeem'
【问题讨论】:
-
您不能使用 POST 创建您已经知道 ID 的资源。如果你想使用 POST,你将不得不使用
:url => coupons_redeem_path。您能否将您的路线包含在优惠券资源中? -
路线已添加
标签: forms controller ruby-on-rails-4