Reputation: 91
I want render html from mysql2. What is wrong? Here is Error code
NoMethodError in Admin::Stores#index
Showing /home/rok/Development/Pan/app/views/admin/stores/index.html.erb
where line #15 raised:
undefined method `name' for #<Hash:0x007f627f0cf808>
Extracted source (around line #15):
13 <% @store_list.each do |stores| %>
14 <tr>
15 <th><%= stores.name %> </th>
16 <td><%= stores.address %></td>
17 <th><%= stores.phone %></th>
18 </tr>
Here is my view code
index.html.erb
<% @store_list.each do |stores| %>
<tr>
<th><%= stores.name %> </th>
<td><%= stores.address %></td>
<th><%= stores.phone %></th>
</tr>
<% end %>
Here is my controller code
class Admin::StoresController < ApplicationController
$client = Mysql2::Client.new(:host => "localhost", :username => "rok", :password => "xxxxxx@", :database => "Pan_development")
def index
@store_list = $client.query('select * from stores')
end
end
my routes
namespace :admin do
resources :stores
end
rake routes
admin_stores GET /admin/stores(.:format) admin/stores#index
POST /admin/stores(.:format) admin/stores#create
new_admin_store GET /admin/stores/new(.:format) admin/stores#new
edit_admin_store GET /admin/stores/:id/edit(.:format) admin/stores#edit
admin_store GET /admin/stores/:id(.:format) admin/stores#show
PATCH /admin/stores/:id(.:format) admin/stores#update
PUT /admin/stores/:id(.:format) admin/stores#update
DELETE /admin/stores/:id(.:format) admin/stores#destroy
Upvotes: 1
Views: 55
Reputation: 34338
undefined method `name' for #<Hash:0x007f627f0cf808>
Looking at your error message, stores
is a Hash. You can't access the hash key/value using .
operator.
You should be able to access the name
of the store by using: stores[:name]
or stores['name]
. See the documentation on how to retrieve the value of a key in a Hash.
So, this should fix your problem:
<% @store_list.each do |stores| %>
<tr>
<th><%= stores['name'] %> </th>
<td><%= stores['address'] %></td>
<th><%= stores['phone'] %></th>
</tr>
<% end %>
Upvotes: 1