开发者

.find() returns hash-symbols!

I have this in one of my /views/ files:

<%= Result.find(:all) %>

开发者_运维知识库Result is a model. This works fine in the console but it returns ['#,'#] in the view. It has recognised that I have two results but its not displaying the results. Any ideas why?


It's not a hash that is being returned, it is the concatenated string representation you are seeing.

<%= %> is the same as puts in the console. Example:

> puts User.all
#<User:0x00000102f98550>
...

If you want to see all the attributes you need to use inspect. Example:

> puts User.all.inspect
[#<User id: 2, email: "ga...

So:

<%= Result.find(:all).inspect %>

Still not going to be pretty output though, I guess you are doing this for debugging.


Or if you want to watch each of all the Results you just should do

<% Result.find(:all) do |result| %>
  <%= result.your_result_attribute %>
<% end %>


Assuming you are using Rails < 3

The reason why you are getting a # is because the next character is a <, your browser is interpreting that as a HTML element as such if you want to quickly see what that is, wrap it in a h()

i.e.

<%= h(Result.find(:all).inspect) %>


Use or debugger to debug or logger.debug to output it in your development.log.

What you are doing here is Result.find(:all).to_s because <%= %> will do a .to_s

To debug this properly:

<% logger.debug "Result.find(:all): #{Result.find(:all).inspect}" %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜