A controller action invokes the update or creation of a record, while having a private method to set the strong params for that object's class, which is different from the originating class.
def userdiscount
articlediscount_params = params[:articlediscount]
initialise_article_and_params(articlediscount_params)
puts articlediscount_params
if articlediscount_params[:id].present?
@articlediscount = Articlediscount.find(articlediscount_params[:id].to_i)
@articlediscount.update!(articlediscount_params)
else
@articlediscount = Articlediscount.create!(articlediscount_params)
end
end
def articlediscount_params
params.require(:articlediscount).permit(:article_id, :user_id, :discounted_price, :discount, :shop_id)
end
The strong params definition is identical to the one in the articlediscounts controller
Why then are tests complaining about the attributes? removing the require
element of the statement does not modify the error
ActiveModel::ForbiddenAttributesError: ActiveModel::ForbiddenAttributesError
CodePudding user response:
The main problem is this line:
articlediscount_params = params[:articlediscount]
The problem is that you now have two objects in this controller called articlediscount_params
- this variable and the method that's actually doing your strong-params filtering. Remove the variable definition and you should find that your code works more as you expect.