Reputation: 936
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 %>
Upvotes: 1
Views: 2710
Reputation: 54882
If you are using Rails 3, I advise you to find object using this method:
# equivalent of find_all
Coupon.where(:coupon => params[:coupon]) # => Returns an array of Coupons
# equivalent of find :first
Coupon.where(:coupon => params[:coupon]).first # => Returns a Coupon or nil
Try to do a params.inspect
to see exactly how is made your Hash. I think it is built like this:
{ :coupon => { :coupon => '100' } }
If it is, you should use params[:coupon][:coupon]
to get the String '100'
Following your update:
semantic_form_for
is creating the form for you, as you give him a Coupon.new
it will build the params this way:
params = {
:coupon => { :attribute_1 => 'value_1', :attribute_2 => 'value_2' }
}
If you prefer to use the find_by method:
Coupon.find_by_coupon(params[:coupon][:coupon]) # => Returns a Coupon or raise a RecordNotFound error
Or with the where method:
Coupon.where(:coupon => params[:coupon][:coupon]).first # => Returns a Coupon or nil
Upvotes: 2