I'm using Rails 4+ MongoDB (mongoId) + money-rails gem and have these 2 models:
class MyModel
field :date, type: Date
field :amount, type: Money
..............
end
class OtherModel < MyModel
........
end
On the controller the permit params functions is as follows:
def my_model_params
base=[:field1, field2, :field3, :amount]
params.permit(:field11, :field22, :field33, :field44, other_model: base)
end
Now ,the amount field is Money type, but before that it was a float field. With float, the create method worked without problems, but now that the :amount field is of type Money, I'm getting on the logfile the message Unpermitted parameter: amount. This happens when I want to create a new OtherModel object:
OtherModel.create(field1: "aaa", field2: "bbb", field3: "ccc", :amount=>Money.new(12345))
I've tried several ways of defining the my_model_params method so that it accepts the amount field, including all the options related to this I found at StackOverflow, but I keep getting the same error. The only way I found of making this work is by using permit!
def my_model_params
base=[:field1, field2, :field3, :amount]
params.permit!
end
Which works because it allows all fields, but it is obviously not a good option for mass assignment protection.
On the hash of params, the amount field comes as follows, due to the money-rails gem:
"amount"=>#<Money fractional:12345 currency:USD>
Has someone found a similar issue or knows how to declare the amount field?
Thanks! Marco
amount
is aMoney
instance beforemy_model_params
sees anything? And everything worked fine whenamount
was a number field? – mu is too shortamount
is aMoney
instance whenmy_model_params
is executed rather than aString
? – mu is too short