Reputation: 348
I have the method below and I get the error
"no implicit conversion of String into Integer"
on line
if (!thisitem['value'].to_i.match(/[\/-].{2}/).nil?)
A Little background: I am new to this project, this is a internal CMS. I am attempting to upgrade from Rails 2 to Rails 4. I come from a ASP.NET background.
Parameters: Parameters: {"utf8"=>"✓", "authenticity_token"=>"ahD3oriEXY895BNx53nd03Q6PQZ1yOQkgkpCGM4OlVqXODjrl3EIb6Uqa9mqVwwWgCQhV5qxRebCmEyoP57HzQ==", "info_items"=>{"1"=>{"id"=>"1", "description"=>"Billing Rate", "value"=>"110"}, "2"=>{"id"=>"2", "description"=>"Travel Rate", "value"=>"55"}}, "commit"=>"Update"}
def update_info
@updated_items=params[:info_items]
@updated_items.each do |thisitem|
@item=TrackInformation.find_by_id(thisitem)
if (!thisitem['value'].match(/[\/-].{2}/).nil?)
thisdate=thisitem['value']
if !thisdate.match(/\/.{2}/).nil?
newdate=Date.strptime(thisdate,"%m/%d/%Y")
else
newdate=Date.strptime(thisdate,"%m-%d-%Y")
end
thisdate=newdate
thisitem['value']=thisdate
end
@item.update_attributes(thisitem)
@item.changed_by=User.find(session[:user]).id
@item.save
end
end
edit:
so reading @Anand I realized that a date should not equal value because value is a dollar amount, so I modified the method to be:
def update_info
i = 1
@updated_items=params[:info_items]
@updated_items.each do |this_item|
@item=TrackInformation.find_by_id(this_item[i][:id])
@item.description = this_item[i][:description]
@item.value = this_item[i][:value]
@item.changed_by=session[:user].to_i
@item.save
i = i + 1
end
redirect_to :controller => 'admin', :action => 'list'
end
Now I get:
undefined method `[]' for nil:NilClass
Edit 2:
def update_info
byebug
@updated_items=params[:info_items]
@updated_items.each do |id, description, value|
@item=TrackInformation.find_by_id(id)
@item.value = value
@item.description = description
@item.changed_by=session[:user]
@item.save
end
redirect_to :controller => 'admin', :action => 'list'
end
this seems to work but puts this in the DB:
Edit 3:
def update_info
@updated_items=params[:info_items]
@updated_items.each do |this_item_key,this_item_value|
@item=TrackInformation.find_by_id(this_item_key)
@item.update_attribute(this_item_key, this_item_value)
@item.changed_by=session[:user]
@item.save
end
redirect_to :action => 'list'
end
Upvotes: 0
Views: 776
Reputation: 3760
Based on your params, each thisitem is a hash - you can get the key and value as block params, and use them appropriately. Also, if !(something).nil?
can be simplified to if something.present?
. Finally, it is a good idea to name variables with underscore - this_item
instead of thisitem
Update: As the question has changed, updated the code below as well
def update_info
@updated_items=params[:info_items]
@updated_items.each do |this_item_key,this_item_value|
@item=TrackInformation.find_by_id(this_item_key)
@item.value = this_item_value['value']
@item.description = this_item_value['description']
@item.changed_by=session[:user]
@item.save
end
end
Upvotes: 1