I have a webshop with an admin user indexpage to check user information. On this page I now want to show the shipping address (with firstname, lastname, street...).
I've managed to create this helper method to get the firstname, by using the user_id, which both the user and shipping_address have:
module Admin::CustomersHelper
def customer(user)
s = ShippingAddress.find_by(id: user)
if s
s.firstname
end
end
end
In my views I have:
<%= customer(@user.id) %>
This works, but the problem is that I need to show 10+ different params, and there's got to be a better way than making 10 different helper methods...
I've also tried to do <%= customer(@user.id).firstname %>, but this gives the error undefined method `firstname' for nil:NilClass.
My Admin::CustomersController:
def index
@users = User.all
@shipping_addresses = ShippingAddress.all
end
Some relevant parts of my schema.rb:
create_table "shipping_addresses", force: :cascade do |t|
t.string "firstname"
t.string "lastname"
t.string "street"
...etc
t.bigint "user_id"
t.index ["user_id"], name: "index_shipping_addresses_on_user_id"
..etc
add_foreign_key "invoice_addresses", "users"
add_foreign_key "order_items", "orders"
add_foreign_key "order_items", "products"
add_foreign_key "orders", "order_statuses"
add_foreign_key "orders", "users"
add_foreign_key "shipping_addresses", "users"
end
You say your code works for displaying one field of customer, but i doubt that.
Your code here is very confusing:
module Admin::CustomersHelper
def customer(user)
s = ShippingAddress.find_by(id: user)
if s
s.firstname
end
end
end
Few points:
Method name suggests that it returns an instance of Customer
or similar model. But It is returning a string OR nil (in cases).
Method accepts an argument under name user, which one would assume to be an instance of User
class. But actually it is an ID.
You are querying on id
field. I think it should be on user_id
.
I would not return a particular field from the method, but the instance of ShippingAddress
. So that, i don't have to call multiple methods to display multiple fields.
So, the updated code would be like this:
module Admin::CustomersHelper
def shipping_address_for(user_id)
ShippingAddress.find_by(user_id: user_id)
end
end
And in views,
<% shipping_address = shipping_address_for(@user) %>
<% if shipping_address %>
<%= shipping_address.firstname %>
<%= shipping_address.lastname %>
...
<% end %>