0

我有以下模型:

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'
4

1 回答 1

0

在您的路线中,couponscompanies. 因此,您应该选择以下替代方案之一:

第一个:

resources :companies do 
  resources :coupons do
    post :redeem, on: :member
  end
end

这会导致这样的助手:(redeem_company_coupon_path(company, coupon)并通过 POST 发送 smth)。

如果您不想将公司包括在您的路径中,您可以选择第二个:

resources :companies do 
  resources :coupons
end   
post 'coupons/:id/redeem', to: 'coupons#redeem', as: :redeem_coupon

之后你可以使用redeem_coupon_path(coupon)助手

于 2013-09-09T05:44:19.270 回答