Hi All,
I am new in using Ruby on Rails, I have a problem about display a pic by
using file_column.
Wish someone can help me, thx
In below code, I want to use @entry to store the Entry object which with
ID=5. (I have already created a entry with ID=5.)
And then I use display.rhtml to display the relative pic
But I get this error message
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.image_relative_path
Extracted source (around line #1):
1: <%= image_tag url_for_file_column ‘entry’, ‘image’ ,‘thumb’%>
Below are the codes I used.
==Controller===
def display
@entry = Entry.find(5)
end
==display.rhtml====
<%= image_tag url_for_file_column ‘entry’, ‘image’ ,‘thumb’%>
Below code is copy from a site which teach how to use file_column.
It’s work !!! I write the other code similar to it, but my code is not
work
==Controller===
def show
@entry = Entry.find(params[:id])
end
==show.rhtml===
<% for column in Entry.content_columns %>
<%= column.human_name %>: <%=h @entry.send(column.name) %>
Original:
<%= image_tag url_for_file_column 'entry', 'image' %>
thumb:
<%= image_tag url_for_file_column 'entry', 'image' ,'thumb'%>
medium:
<%= image_tag url_for_file_column 'entry', 'image' ,'medium'%>
<% end %>
<%= link_to 'Edit', :action => 'edit', :id => @entry %> |
<%= link_to 'Back', :action => 'list' %>
Hi,
My guess is that you need to put parenthesis around the parameters of
url_for_file_column. So it would look like <%= image_tag
url_for_file_column( ‘entry’, ‘image’ ,‘thumb’ ) %>
Otherwise Rails can’t know if those parameters are for the image_tag
method or for the other one.
Hi Jouni,
Thx for your help, but the code still not work.
After read some post in the site, I find that it’s required to use @ to
define the local variable from controller and past it to the view.
And then, I try to change code to below.
But, the other error message is diplayed.
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):
1: <% for @entry in @entries %>
2: <%= image_tag url_for_file_column(‘entry’, ‘image’ ,‘thumb’)%>
3:
4: <% end %>
==Controller===
def display
@entries = Entry.find(:all)
end
==display.rhtml====
<% for @entry in @entries %>
<%= image_tag url_for_file_column(‘entry’, ‘image’ ,‘thumb’)%>
<% end %>
Stanley
Jouni Pohjolainen wrote:
Hi,
My guess is that you need to put parenthesis around the parameters of
url_for_file_column. So it would look like <%= image_tag
url_for_file_column( ‘entry’, ‘image’ ,‘thumb’ ) %>
Otherwise Rails can’t know if those parameters are for the image_tag
method or for the other one.
are you sure, that @entries can find something?
put something like
logger.info “foo: #{@entries.size}”
in your controller and check development.log
Hi Thorsten,
Thx for your feedback
I have add this logger.info to the display method in the controller
def display
logger.info(“haha”)
@entries = Entry.find(:all)
logger.info(“foo: #{@entries.size}”)
end
The log show nothing. But I add the same log code in the method “list”
which generated by scaffold,
It can show the correct log.
def list
logger.info(“haha”)
@entry_pages, @entries = paginate :entries, :per_page => 10
logger.info(“foo: #{@entries.size}”)
end
Why ???
Thanks a lot
Stanley
Thorsten M. wrote:
are you sure, that @entries can find something?
put something like
logger.info “foo: #{@entries.size}”
in your controller and check development.log
Hi Thorsten,
Finally, I solve the problem.
I create another method with the other name, but use the same code.
It’s WORK !!!
Maybe the name “display” is a reserve word in RoR, and cannot be use as
the method name
Anyway, thx a lot
Stanley
Stanley Wong wrote:
Hi Thorsten,
Thx for your feedback
I have add this logger.info to the display method in the controller
def display
logger.info(“haha”)
@entries = Entry.find(:all)
logger.info(“foo: #{@entries.size}”)
end
The log show nothing. But I add the same log code in the method “list”
which generated by scaffold,
It can show the correct log.
def list
logger.info(“haha”)
@entry_pages, @entries = paginate :entries, :per_page => 10
logger.info(“foo: #{@entries.size}”)
end
Why ???
Thanks a lot
Stanley
Thorsten M. wrote:
are you sure, that @entries can find something?
put something like
logger.info “foo: #{@entries.size}”
in your controller and check development.log
Yeah, you are right about it being reserved word in Rails. Well
actually not reserved, but reported to cause problems. Good site to
check those reserved and problematic names is this.
http://wiki.rubyonrails.org/rails/pages/ReservedWords