I have a coupon system, and I’m trying to get the coupon object with the method find_by:
Coupon.find_by_coupon(params[:coupon])
I’m getting this error:
ArgumentError Exception: Unknown key: coupon
I’m sure params[:coupon] is right:
(rdb:1) eval params[:coupon]
{"coupon"=>"100"}
I have the following model:
# Table name: coupons
#
# id :integer not null, primary key
# coupon :string(255)
# user_id :integer
UPDATE:
It’s working if I put Coupon.find_by_coupon(params[:coupon][:coupon]) instead of Coupon.find_by_coupon(params[:coupon]).
Here the code with the form in my view:
<%= semantic_form_for Coupon.new, url: payment_summary_table_offers_path(@booking_request) do |f| %>
<%= f.input :coupon, :as => :string, :label => false, no_wrapper: true %>
<%= f.action :submit, :as => :button, :label => t(:button_use_coupon), no_wrapper: true,
button_html: { value: :reply, :disable_with => t(:text_please_wait) } %>
<% end %>
If you are using Rails 3, I advise you to find object using this method:
Try to do a
params.inspectto see exactly how is made your Hash. I think it is built like this:If it is, you should use
params[:coupon][:coupon]to get the String ‘100’Following your update:
semantic_form_foris creating the form for you, as you give him aCoupon.newit will build the params this way:If you prefer to use the find_by method:
Or with the where method: