views:

102

answers:

3

Hi, I want to display the column 'name' after being found from the table mytest.

In the mytest.rb, I defined "attrib_accessor :name";

In the procedure index under directory /controller/mytest_controller.rb,

def index
###[Ignore some code]
@[email protected]_by_id 
### I am able to verify the tuples in @mytesttbl
end 

In the /view/mytest/index.rhtml, I have code like

<div id="mytesttable"> <%= render(:partial =>"mytesttbl", :object => @mytesttbl)%> </div> 

In the /view/mytest/_mytesttbl.html.erb. I have code like

<tr>
 <td><%=mytestbl.name %></td>
<tr>

when I ran the above code, I have error on _mytesttbl.html.erb,

undefined method 'name ' for #<Array:0xb6c971cc>

Please help. Thanks,

+1  A: 

mytesttbl is an array. Change view/mytest/index.rhtml to

<% @mytesttbl.each do |obj| %>
  <div id="mytesttable">
   <%= render(:partial =>"mytesttbl", :object => obj)%>
  </div>
<% end %>

Edit: Also, view/mytest/_mytesttbl.html.erb should look like

<tr>
 <td><%= object.name %></td>
<tr>
anshul
Hi, I replaced the code in mytesttbl.html.erb as below,In the /view/mytest/_mytesttbl.html.erb. I have code like <tr> <td><%=obj.name %></td> <tr>I got error, "undefined local variable or method `obj' for #<ActionView::Base:0xb6c9f930>"
erwin
Does it really work like that? I'd expect it to need "each do |@obj|"
Trevoke
** Trevoke ** I think it should be :object => obj not @obj
Steve Weet
Hi, thx for your replies. I tried with/out @ in index.rhtml, the "obj" is not defined in _mytesttbl.html.erb. It reported the same error.
erwin
@erwin: Try the updated code and please do make sure that you understand what was wrong with your version of it.
anshul
It works now. I found out that using :collection can remove the loop from the suggestion above. Here is a good reference for using render. Thanks again, you guys are awesome. http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
erwin
+2  A: 

I'm guessing that's not your actual code since it wouldn't actually work. Your find is returning an array. You either want to loop through the array or, if you know it contains only one object (as in the case where you actually are finding by a unique id), you can just do mytesttbl.first to get the object out of it.

Chuck
Hi, I tried to loop to publish the names in the array with your suggestion. I replaced the code in mytesttbl.html.erb as below, In the /view/mytest/_mytesttbl.html.erb. I have code like <tr> <td><%=obj.name %></td> <tr> I got error, "undefined local variable or method `obj' for #<ActionView::Base:0xb6c9f930>". What is the wrong? Thx,
erwin
@erwin: You don't have a variable named "obj".
Chuck
I used the code below. How to pass the obj to _mytesttbl.html.erb? thx<% @mytesttbl.each do |obj| %> <div id="mytesttable"> <%= render(:partial =>"mytesttbl", :object => @obj)%> </div><% end %>
erwin
Then your variable in partial is called `object`, not `obj`. You can also use `<%= render :partial => 'mytesttbl', :collection => @mytesttbl %>`. Your object will have name `mytesttbl` in partial code (it the name of a partial).
klew
Actually, I think the variable would be called `mytesttbl`, since the `:object` parameter translates into a variable named after the partial.
Chuck
Hi, thx for your replies. I replaced ":object=>@mytesttbl" with ":collection=>@mytesttbl" in index.rhtml, there was not compiling error, but instead of displaying the names in the web browser, the _mytesttbl.html.erb showed the same number of EMPTY line as the number of name in the @mytesttbl. I was able to use 'puts' to verify the name in console. Why were the names not shown in web browser from _mytesttbl.html.erb? thx
erwin
Thanks for your help. I found out that 'attr_accessor :name' in /model/mytest.rb caused the problem. Once I removed that line of statement, the "name" was showned on the web browser. I donot know why?
erwin
A: 

arent you missing an argument for the @user.find_by_id? usually when i want to get the name i do:

controller @user= User.find(params[:id]) to find a user by a specific id.

view <%= @user.name %>

if you want to find all users then

controller @user = User.find(:all)

view <% @user.each do |user| %> <% user.name %> <% end %>

User.find_by_id is redudant because User.find by default finds by id.

Mike