Given these paramters:
{
"items"=>{"id"=>"", "id"=>"", "id"=>""}
}
How would I go about assigning the content for each item in rails?
This is what I have tried:
params[:items].each do |i|
item = Item.find(i)
@content = params[:s]
end
end
It obviously cannot find any value for params[:s], so @content is equals nil.
I have absolutely no idea on how to get this value. I appreciate each answer. Thank you!
Take a look at the documentation for the
Hashclass, specifically theeachmethod. There are two parameters in the block, a key and a value. So adding that second parameter will allow you to get that value:I'm not sure why you're setting
@contentrepeatedly. That's the same variable throughout the whole script; are you trying to set acontentattribute on eachitem? If so, you just need to sayitem.content = value(and probably saveitemat some point, if you want it to persist in the database). And you wouldn't need to put that within a block forItem.find; just put it on the next line(s) within the outer block. (If you did need a block forItem.find, you appear to be missing adoon that line.)