user532339
user532339

Reputation:

Ruby on rails .try() not working correctly

I am trying to do the following:

    <%= order.user.try(:first_name) %>
    <%= order.user.try(:last_name) %>
    <%= order.user.try(:email) %>

This works however when I view the index of this page, the data does not appear in the table. I also viewed the logs and saw that the relevant data is being posted

Started POST "/orders" for 127.0.0.1 at 2013-02-18 11:15:02 +0000 Processing by OrdersController#create as HTML Parameters: {"utf8"=>"?", "authenticity_token"=>"Rjf7+Z8UAPh9ZofIrOs+p6AoGPfgd/6rfdTRqJLtx0g=", "user"=>{"first_name"=>"Test", "last_name"=>"TestHome", "email"=>"[email protected]"}, "order"=>{"address"=>"3433234243", "card_number"=>"45423554213994", "card_type"=>"visa", "card_expires_on(1i)"=>"2013", "card_expires_on(2i)"=>"2", "card_expires_on(3i)"=>"1"}, "commit"=>"Confirm Order"} Cart Load (0.0ms) SELECT carts.* FROM carts WHERE carts.id = 8 LIMIT 1

so I alternatively tried to do

    <%= order.user.first_name %>
    <%= order.user.last_name %>
    <%= order.user.email %>

Doing this resulted in

NoMethodError in Orders#index

Showing C:/Users/sites/e-smart/app/views/orders/index.html.erb where line #18 raised:

undefined method `first_name' for nil:NilClass

I understand that the result of this error is because first_name is empty.

What is the best way to get the data.

Order.rb

class Order < ActiveRecord::Base
  attr_accessible :address, :card_expires_on, :card_type, :ip_address, :card_number, :first_mame, :last_name, :email, :quantity
  belongs_to :cart 
  has_many :line_items, :dependent => :destroy   
  belongs_to :user

User.rb

class User < ActiveRecord::Base

  has_and_belongs_to_many :roles
  has_many :role_users
  has_many :orders

Upvotes: 0

Views: 439

Answers (3)

Raghavan Kandala
Raghavan Kandala

Reputation: 299

Let me clarify the meaning of this error first.

  NoMethodError in Orders#index

  Showing C:/Users/sites/e-smart/app/views/orders/index.html.erb where line #18 raised:

  undefined method `first_name' for nil:NilClass

The above error doesn't mean that first_name for user is empty, but the user object itself is nil. It means that, your @order.user is nil and not @order.user.first_name is empty.

So, order.user.try(:first_name) should be read as print the first_name of the user who placed this order if there is an user associated with the order, otherwise do nothing but don't throw any error

The parameters in the form are being sent properly to your controller and may be you are missing something in the controller that associates the order and user records. Look into the controller action that is saving the object and make sure you are associating order and user properly.

Hope this helps

Upvotes: 1

emrass
emrass

Reputation: 6444

You are saying:

I understand that the result of this error is because first_name is empty.

This is not correct. The cause of the error is that user is nil. This is also in line with the behavior or try() to return -blank-.

Make sure you have an order with the user populated correctly. This means your order table must have a user_id and the user must be assigned correctly.

Upvotes: 3

sevenseacat
sevenseacat

Reputation: 25049

You're comparing two different things here - the try is working correctly in your view, it's returning nil when you call it on nil (undefined method 'first_name' for nil:NilClass is because order.user is nil, not because first_name is nil.

This is unrelated to your POST - what is the actual issue you're trying to solve here?

Upvotes: 1

Related Questions