0

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: enter image description here

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

enter image description here

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

try replacing your code with mine. you are still getting one param in the block, when you need to get two - key and value.
No, you haven't tried what I said - you have arbitrarily put three parameters within the Hash#each block - it does not work that way. Read up on Hash#each - ruby-doc.org/core-2.2.0/Hash.html#method-i-each
okay, I apologize I wasn't quite understanding. I had to change "update_attributes" to "update_attribute" because "update_attributes" only accepts one argument, I posted a update above.
I have updated the response based on the latest edit to the question. The question seems very different from what it looked like in the beginning. Let me know if this code works for you. If not, let me know what exactly you want to achieve in the update_info method (earlier it seemed like you wanted to update some single attribute, later it looks like you want to update multiple attributes of the item - value, description).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.