Has Many through with simpleform

左心房为你撑大大i 提交于 2019-12-11 20:10:52

问题


I'm struggling to get going with a simpleform field for a hasmany through association. When i submit the form the has_many records are not saved, and if the form doesn't validate the options are not selected when the form redisplays. so in short, nothing is working.

I'm not sure how much automagic should be going on here, or if I am just missing code i need to make it work. I don't think it's a strong parameters issue as I am not getting a permit params error in the logs.

model

class Coupon < ActiveRecord::Base

    belongs_to :provider
    has_many :reductions
    has_many :activities, through: :reductions,  :autosave => true
...

view

<%= f.association :activities, collection: current_user.provider.activities.collect %>

controller

def update
  @coupon = Coupon.find(params[:id])
  if @coupon.update_attributes(coupon_params)
    redirect_to helm_coupons_path, notice: 'Coupon was successfully updated.'
  else
    render  "edit"
  end     
end
....
def coupon_params
  params.require(:coupon).permit(:provider_id, :code, :max_uses, :max_uses_per_customer, :name, :expires_at, 
    :dollar_discount, :percent_discount, :starts_at, :activity_ids => {})
end

Where am I going wrong?

UPDATE: log of create action as requested in comments:

Started POST "/helm/coupons" for 127.0.0.1 at 2014-08-24 09:58:25 +1000

Processing by Helm::CouponsController#create as HTML

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h8OjO3zqPVofhgQ8VYECea4kCCnXU+Lkn7L2QPJUDcM=", "coupon"=>{"name"=>"test", "code"=>"test", "dollar_discount"=>"5", "percent_discount"=>"", "max_uses"=>"1000000", "max_uses_per_customer"=>"1000000", "starts_at(1i)"=>"2014", "starts_at(2i)"=>"8", "starts_at(3i)"=>"24", "starts_at(4i)"=>"09", "starts_at(5i)"=>"00", "expires_at(1i)"=>"2017", "expires_at(2i)"=>"8", "expires_at(3i)"=>"24", "expires_at(4i)"=>"09", "expires_at(5i)"=>"00", "activity_ids"=>["", "2", "3", "4"]}, "commit"=>"Saving..."}

  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Provider Load (0.4ms)  SELECT  "providers".* FROM "providers"  WHERE "providers"."id" = $1 LIMIT 1  [["id", 1]]
  Provider Load (0.5ms)  SELECT  "providers".* FROM "providers"  WHERE "providers"."id" = $1 LIMIT 1  [["id", 1]]
   (0.3ms)  BEGIN
  Coupon Exists (0.5ms)  SELECT  1 AS one FROM "coupons"  WHERE "coupons"."code" = 'test' LIMIT 1
  SQL (34.0ms)  INSERT INTO "coupons" ("code", "dollar_discount", "expires_at", "max_uses", "max_uses_per_customer", "name", "percent_discount", "provider_id", "starts_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["code", "test"], ["dollar_discount", 5.0], ["expires_at", "2017-08-23 23:00:00.000000"], ["max_uses", 1000000], ["max_uses_per_customer", 1000000], ["name", "test"], ["percent_discount", ""], ["provider_id", 1], ["starts_at", "2014-08-23 23:00:00.000000"]]
   (0.6ms)  COMMIT
   (0.3ms)  BEGIN
   (0.3ms)  COMMIT
   (0.3ms)  BEGIN
  Coupon Exists (0.9ms)  SELECT  1 AS one FROM "coupons"  WHERE ("coupons"."code" = 'test' AND "coupons"."id" != 1) LIMIT 1
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/helm/coupons
Completed 302 Found in 117ms (ActiveRecord: 39.1ms)
cache: [POST /helm/coupons] invalidate, pass

来源:https://stackoverflow.com/questions/25459673/has-many-through-with-simpleform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!