Reputation: 485
I have a order show page shown below. I had this working but ever since I changed computers to work on this app, the name did not display.
If I change <%= @order.user.name %>
to <%= @order.user.id %>
, I get the correct id. Otherwise I get nothing displaying where as the name of the id should be posted.
<p>
<strong>Code:</strong>
<%= @order.code %>
</p>
<p>
<strong>Client:</strong>
<%= @order.client.name %>
</p>
<p>
<strong>User:</strong>
<%= @order.user.name %>
</p>
<p>
<strong>Notes:</strong>
<%= @order.memo %>
</p>
<p>
<strong>Status:</strong>
<%= @order.status ? "Confirmed" : "In Progress" %>
</p>
<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>
class Order < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :orders, dependent: :destroy
has_many :addresses, dependent: :destroy
has_many :telephones, dependent: :destroy
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enum role: [:user, :manager, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
end
def create
@order = Order.new(order_params)
@order.user_id = current_user.id
@order.status = TRUE
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: @order }
else
format.html { render :new }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
Upvotes: 2
Views: 4575
Reputation: 1250
It's completely independent of the machine you use, so there have to be some changes you've made in your code that you don't remember.
Check what value is returned when you call this user from the console. Type rails c
in the terminal and then type a command:
User.find(<<ID_OF_USER_WHOES_NAME_IS_NOT_DISPLAYED>>).name
If its name is empty, it could be a problem with creating a user or something.
Make update when you'll check it.
Upvotes: 2
Reputation: 76774
I think I just answered another question of yours, but I would recommend using the .delegate
method like this:
#app/models/order.rb
Class Order < ActiveRecord::Base
belongs_to :user
delegate :name, to: :user, prefix: true #-> @order.user_name
end
You can delegate multiple variables if you need to. This fixes Law of Dementer, as described here
Upvotes: 3
Reputation: 485
As mentioned in comments, the value for the user name is empty and that is why it displays a blank when looking for the name.
Adding a name(string) to the user.name will solve the issue
When using a different database, make sure to also update the database with data, preferably the same data which initially tested with.
Upvotes: 2