Forum: Ruby on Rails image text link instead of image_for help!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
792ae44d201909299282ab8dd5028546?d=identicon&s=25 denver (Guest)
on 2008-12-03 12:36
(Received via mailing list)
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 href=@article.image ...??

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
-------------------------------------------------------------------
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-03 22:42
(Received via mailing list)
On Dec 3, 11:00 am, denver <louis.ba...@cyber-craft.co.uk> 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
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-04 14:14
Frederick Cheung wrote:
> On Dec 3, 11:00�am, denver <louis.ba...@cyber-craft.co.uk> 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:
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-04 14:28
(Received via mailing list)
On 4 Dec 2008, at 13:14, Dave Smith 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
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-04 14:36
Frederick Cheung wrote:
> On 4 Dec 2008, at 13:14, Dave Smith 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:in `article_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:in
`send'
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:in
`send'
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:in
`render'
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:in
`realtime'
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:in
`render'
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:in
`render_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:in `each'
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:in
`send'
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:in
`send'
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:in
`render'
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:in
`render_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:in
`render_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:in
`render_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:in
`realtime'
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:in
`send'
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:in
`call'
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:in
`each'
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:in
`respond_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:in
`send'
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:in
`call_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:in
`perform_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:in
`perform_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:in
`perform_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:in
`cache'
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:in
`send'
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:in
`process_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:in
`process'
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:in
`dispatch'
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:in
`dispatch'
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:in
`dispatch'
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:in
`synchronize'
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:in
`process_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:in
`process_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:in
`initialize'
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:in
`run'
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:in
`new'
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:in
`run'
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:in
`run'
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:in
`run'
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:in
`load'
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:in
`load'
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:in
`require'
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:in
`new_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:in
`gem_original_require'
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-04 14:39
(Received via mailing list)
On 4 Dec 2008, at 13:36, Dave Smith 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
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-04 14:53
Frederick Cheung wrote:
> On 4 Dec 2008, at 13:36, Dave Smith 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...
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-04 15:01
(Received via mailing list)
On 4 Dec 2008, at 13:53, Dave Smith 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
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-04 15:37
Frederick Cheung wrote:
> On 4 Dec 2008, at 13:53, Dave Smith 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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-04 15:43
(Received via mailing list)
On Dec 4, 2:37 pm, Dave Smith <rails-mailing-l...@andreas-s.net>
wrote:
> Frederick Cheung 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
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-04 15:49
Frederick Cheung wrote:
> On Dec 4, 2:37�pm, Dave Smith <rails-mailing-l...@andreas-s.net>
> wrote:
>> Frederick Cheung 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?
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2008-12-04 17:35
Dave Smith wrote:
>
> booger.... is there a potential workaround?

Umm... don't use that helper?  You can always construct the control
yourself.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2008-12-04 18:11
(Received via mailing list)
Hi Dave,

Dave Smith wrote:

> On Dec 4, 2:37�pm, Dave Smith <rails-mailing-l...@andreas-s.net>
> wrote:
>> Frederick Cheung 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 <snip>

> 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
This topic is locked and can not be replied to.