Image text link instead of image_for help!

Hi Guys,

I am using attatchment_fu to adding images to my rails app, and have
the following

<%= article_image_for(@article) %>

which displays the image for my article… however i just want a text
link to the image instead of the image itself…

can this be done with a link_to? or an <a [email protected] …??

i know this is probably a simple thing to do!! cheers…!!

Here is my code…


articles_helper

def article_image_for(article)
if article.article_image
article_img = article.article_image.public_filename
link_to image_tag(article_img),
article.article_image.public_filename
#else
#image_tag(“blank-image.png”)
end
end

article_images table

id article_id size width height content_type
filename created_at
1 3 4509 500 400 image/jpg
xrve.jpg 2008-08-27 14:21:15

articles model

class Article < ActiveRecord::Base
has_one :article_image, :dependent => :destroy
has_many :comments
end

On Dec 3, 11:00 am, denver [email protected] wrote:

def article_image_for(article)
if article.article_image
article_img = article.article_image.public_filename
link_to image_tag(article_img),
article.article_image.public_filename

Is it not as simple as changing the above to
link_to ‘click me’, article_img

?

Fred

Frederick C. wrote:

On Dec 3, 11:00�am, denver [email protected] wrote:

def article_image_for(article)
� � � � if article.article_image
� � � � � � article_img = article.article_image.public_filename
� � � � � � link_to image_tag(article_img),
article.article_image.public_filename

Is it not as simple as changing the above to
link_to ‘click me’, article_img

?

Fred

i see what you mean…

i have made changes but now get the error…

Showing admin/articles/edit.html.erb where line #12 raised:

undefined method `stringify_keys’ for
“/article_images/0000/0047/Photo_59.jpg”:String
Extracted source (around line #12):

9:
10: <% end %>
11: <% if(@article.article_image) %>
12: <%= article_image_for(@article) %>
13:
14:
15:

On 4 Dec 2008, at 13:14, Dave S. wrote:

i have made changes but now get the error…

Showing admin/articles/edit.html.erb where line #12 raised:

undefined method `stringify_keys’ for
“/article_images/0000/0047/Photo_59.jpg”:String
Extracted source (around line #12):

What’s the rest of that stack trace?

Fred

Frederick C. wrote:

On 4 Dec 2008, at 13:14, Dave S. wrote:

i have made changes but now get the error…

Showing admin/articles/edit.html.erb where line #12 raised:

undefined method `stringify_keys’ for
“/article_images/0000/0047/Photo_59.jpg”:String
Extracted source (around line #12):

What’s the rest of that stack trace?

Fred

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/helpers/url_helper.rb:201:in
link_to' app/helpers/admin/articles_helper.rb:6:inarticle_image_for’
app/views/admin/articles/_show.html.erb:7:in
_run_erb_47app47views47admin47articles47_show46html46erb' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:342:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:342:in
execute' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template_handlers/compilable.rb:29:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template_handlers/compilable.rb:29:in
render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/partial_template.rb:20:inrender’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:26:in
benchmark' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/benchmark.rb:8:inrealtime’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:26:in
benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/partial_template.rb:19:inrender’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template.rb:22:in
render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/partials.rb:110:inrender_partial’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:277:in
render' app/views/admin/articles/index.html.erb:26:in_run_erb_47app47views47admin47articles47index46html46erb’
vendor/plugins/paginating_find/lib/paging_enumerator.rb:45:in each' vendor/plugins/paginating_find/lib/paging_enumerator.rb:45:ineach’
app/views/admin/articles/index.html.erb:24:in
_run_erb_47app47views47admin47articles47index46html46erb' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:342:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:342:in
execute' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template_handlers/compilable.rb:29:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template_handlers/compilable.rb:29:in
render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template.rb:35:inrender’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/template.rb:22:in
render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_view/base.rb:248:inrender_file’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:1112:in
render_for_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:869:inrender_with_no_layout’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:884:in
render_with_no_layout' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/layout.rb:251:inrender_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:51:in
render' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/benchmark.rb:8:inrealtime’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:51:in
render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:131:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:131:in
custom' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:160:incall’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:160:in
respond' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:154:ineach’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:154:in
respond' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/mime_responds.rb:107:inrespond_to’
app/controllers/admin/articles_controller.rb:16:in index' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:1166:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:1166:in
perform_action_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:579:incall_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:572:in
perform_action_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/rescue.rb:201:in
perform_action_without_caching' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/caching/sql_cache.rb:13:inperform_action’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in
cache' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/query_cache.rb:8:incache’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/caching/sql_cache.rb:12:in
perform_action' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:in
process_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:568:inprocess_without_session_management_support’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/session_management.rb:130:in
process' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:389:inprocess’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:149:in
handle_request' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:107:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:in
synchronize' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:120:in
dispatch_cgi' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:35:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:76:in
process' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:insynchronize’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:in
process' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:159:inprocess_client’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in
each' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:inprocess_client’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in
run' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:ininitialize’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in
new' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:inrun’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in
initialize' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:innew’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in
run' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:282:inrun’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in
each' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:inrun’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel_rails:128:in
run' c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/command.rb:212:inrun’
c:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel_rails:281
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:503:in
load' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:503:inload’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:355:in
new_constants_in' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:503:inload’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/servers/mongrel.rb:64
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:510:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:355:innew_constants_in’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:510:in
require' c:/ruby/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/server.rb:39 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:ingem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require’
script/server:3

Frederick C. wrote:

On 4 Dec 2008, at 13:36, Dave S. wrote:

“/article_images/0000/0047/Photo_59.jpg”:String
Extracted source (around line #12):

What’s the rest of that stack trace?

Fred

looks like you’re passing that string to link_to twice.

Fred

ummm interesting…

module Admin::ArticlesHelper
def article_image_for(article)
if article.article_image
article_img = article.article_image.public_filename
# link_to image_tag(article_img),
article.article_image.public_filename

  link_to 'click me', article_img, 

article.article_image.public_filename
#else
#image_tag(“blank-image.png”)
end
end
end

this is my adapted articles helper method…

On 4 Dec 2008, at 13:53, Dave S. wrote:

link_to image_tag(article_img),

article.article_image.public_filename

 link_to 'click me', article_img,

article.article_image.public_filename

Well you are.
Just
link_to ‘click me’, article_img

is quite enough.

Fred

On 4 Dec 2008, at 13:36, Dave S. wrote:

“/article_images/0000/0047/Photo_59.jpg”:String
Extracted source (around line #12):

What’s the rest of that stack trace?

Fred

looks like you’re passing that string to link_to twice.

Fred

Frederick C. wrote:

On 4 Dec 2008, at 13:53, Dave S. wrote:

link_to image_tag(article_img),

article.article_image.public_filename

 link_to 'click me', article_img,

article.article_image.public_filename

Well you are.
Just
link_to ‘click me’, article_img

is quite enough.

Fred

OK I understand now.

As a quick secondary question, I was wondering why when I have a button
to delete the image from the database it works perfectly in the
edit.html.erb file, but when I move the button into the _form.html.erb
file so that it sits next to the other image related elements the
@article_image.id value is not passed accross… in fact I dont think it
even is picked up in the first place.

<%= button_to “Delete Image”, { :action => “delete_article_image”,
:article_image_id => @article_image.id } %>

def delete_article_image
@article_image = ArticleImage.find(params[:article_image_id])
@article_image.destroy

respond_to do |format|
  format.html { redirect_to(admin_articles_url) }
  format.xml  { head :ok }
end

end

As is the code for the button works on the edit page (which holds the
partial _form) but sits outside the rest of the elements and looks
rediculous having a “delete image” button at the bottom of the page.

On Dec 4, 2:37 pm, Dave S. [email protected]
wrote:

Frederick C. wrote:

As a quick secondary question, I was wondering why when I have a button
to delete the image from the database it works perfectly in the
edit.html.erb file, but when I move the button into the _form.html.erb
file so that it sits next to the other image related elements the
@article_image.id value is not passed accross… in fact I dont think it
even is picked up in the first place.

Probably because behind the scenes the button helper creates a form
and you can’t nest forms

Fred

Frederick C. wrote:

On Dec 4, 2:37�pm, Dave S. [email protected]
wrote:

Frederick C. wrote:

As a quick secondary question, I was wondering why when I have a button
to delete the image from the database it works perfectly in the
edit.html.erb file, but when I move the button into the _form.html.erb
file so that it sits next to the other image related elements the
@article_image.id value is not passed accross… in fact I dont think it
even is picked up in the first place.

Probably because behind the scenes the button helper creates a form
and you can’t nest forms

Fred

booger… is there a potential workaround?

Dave S. wrote:

booger… is there a potential workaround?

Umm… don’t use that helper? You can always construct the control
yourself.

Hi Dave,

Dave S. wrote:

On Dec 4, 2:37�pm, Dave S. [email protected]
wrote:

Frederick C. wrote:

As a quick secondary question, I was wondering why
when I have a button to delete the image from the
database it works perfectly in the edit.html.erb file, but
when I move the button into the _form.html.erb file so
that it sits next to the other image related elements

Probably because behind the scenes the button helper creates a form
and you can’t nest forms

Fred

booger… is there a potential workaround?

Several. Al’s given you one. Here’s two more…

  1. Use your existing code, but separate structure from presentation.
    Fix
    the structure by putting the button (i.e., a form with no input fields)
    outside the form tags and use CSS to position it.

  2. Modify your existing code to use link_to or a variation with images
    instead of text

HTH,
Bill