[英]How to pass the attributes in ajax which can be accessed through params in the controller in ruby on rails?
[英]How to loop through attributes in rails controller params
我有一個has_many關系,我想在每個鏈接到主模型的項目上循環某個字段(配方上的成分)。 我試圖在before_filter中做到這一點,以便我可以正確地將用戶輸入解析為我想要的內容。 我該如何設置do循環來獲取我想要的參數
可通過params
哈希訪問Rails中的請求參數。 您可以使用迭代器遍歷哈希。
params.each do |k,v|
puts "#{k}: #{v}"
end
至於解析用戶的輸入,我個人不會在before_filter
方法中這樣做。 您在下面給出了將“ 1 1/2”之類的輸入轉換為1.5的示例。 嘗試這樣的事情:
Ingredient.rb:
# amount :decimal (assuming this column exists on the ingredients table)
def humanized_amount
amount.humanize!
end
def humanized_amount=(value)
amount = cast_humanized_value_to_decimal(value)
end
然后將humanized_amount
用作表單輸入的屬性。 只是一個建議。 確實沒有正確的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.