display random attachment (paperclip or sortable table?)

35 views
Skip to first unread message

hobo_hippy

unread,
Jun 30, 2011, 8:40:48 PM6/30/11
to Hobo Users
I have a model, let's call it foo, which has a paperclip photo
attachment. I know I can edit the foo card as such:

<%= image_tag @this.photo.url %>

which would render whatever photo attachment is associated with this
card. This is cool :)

However, I'd like to do something similar on the front page!

On my front-page within a <section>...</section> I'd like to dispay a
random image. that is to say, i'd like to make a call similiar to the
one above that chooses a random foo and displays that foo's image.


BTW: I'm tyring to work my way to building a site similar to
kittenwars.com. When I get it all working, I look forward to
submitting a recipe to the hobo cookbook! :p

Thanks,
HoboHippy

hobo_hippy

unread,
Jun 30, 2011, 8:53:33 PM6/30/11
to Hobo Users
forgot to mention - I'm not sure whether I should be approaching this
with ruby code, hobo code (which would be awesome), via paperclip
somehow, or via sortable tables...

kevinpfromnm

unread,
Jul 1, 2011, 12:09:15 PM7/1/11
to hobo...@googlegroups.com
the trick is getting the random foo.  perhaps grab with a limit 1, offset 0 - count?

everything else is straightforward dryml

hobo_hippy

unread,
Jul 5, 2011, 9:48:07 PM7/5/11
to Hobo Users
If foo had a title field, i thought I could show it like this in the
front page index:

<view with="&@foo.title"/>

but that's not working... sorry I'm still learning ruby and such... i
know the & means execute this ruby code.

how could I approach this in dryml? I've been going through a lot of
tutorials and I'm not clear yet. Rememeber, I'm not including this on
any foo page, it's on the front-page.

Furthermore, becuase the photo is attached with

has_attached_file :photo

I have no idea how to access that since all the examples seem to show
how to access things declared within the fields_do section of a model
definition.

I have little to no idea what you mean when you say "grab with limit
1, offset 0 -count?"

:(

kevinpfromnm

unread,
Jul 6, 2011, 12:33:19 PM7/6/11
to hobo...@googlegroups.com
For the code, you probably want a card.  Then define your card to include the picture.   Check this recipe for an example of how to display a paperclip image in a card.  http://cookbook-staging.hobocentral.net/recipes/46-using-lightbox-with-hobo

For the random selection I meant selecting the model with a find or scope, you want 1 item, :limit => 1, but a random selection from the whole collection, so :offset => some_random_number_between_0_and_the_max.  Once you have your item, you should be able to just add a card (after you adjust it to show the image) with that item as the context.

In the controller:
@image = ImageModelCount.find :first, :limit => 1, :offset => rand(ImageModelName.count)

There might be a cleaner way to do the offset without requiring the extra db hit with sql.

In the view:
<card with="&@image" />

hobo_hippy

unread,
Jul 8, 2011, 8:57:56 PM7/8/11
to Hobo Users
THANK YOU!! Thats very clear and concise.

On Jul 6, 12:33 pm, kevinpfromnm <kevinpfro...@gmail.com> wrote:
> For the code, you probably want a card.  Then define your card to include
> the picture.   Check this recipe for an example of how to display a
> paperclip image in a card.  http://cookbook-staging.hobocentral.net/recipes/46-using-lightbox-wit...

hobo_hippy

unread,
Jul 9, 2011, 12:32:13 AM7/9/11
to Hobo Users
I'M still Messing with this... haven't quite got it down.

I think my problem is with how I handled the controller. I followed
your above formulation, except I'm not sure what the imagemodelcount
comes from, or should be. I just stuck in the model name, which of
course yielded nothing good...

I should also mention I added that in the frontController, which I
thought made sense since that's the page I'm trying to get this to
work on...

kevinpfromnm

unread,
Jul 11, 2011, 12:36:14 PM7/11/11
to hobo...@googlegroups.com
Doh.  It should just be the ImageModel name, don't know why I stuck Count on the end of that.

hobo_hippy

unread,
Jul 23, 2011, 4:05:55 AM7/23/11
to Hobo Users
Kevin,

I've been reading and rereading the manual and going over your
recommendations above with no luck. I'm completely stuck and
confused!! please advise. I've included the setup I'm using in a dummy
site I'm messing with to figure this out. Personally, I'm convinced
it's that I'm doing something stupid in setting up the controller.

controller:
____________________________________________
class ThingsController < ApplicationController

hobo_model_controller

auto_actions :all

def show
@rando = Thing :first, :limit => 1, :offset =>
rand(Thing.count)
end

end


model:
____________________________________________

class Thing < ActiveRecord::Base

hobo_model # Don't put anything above this

fields do
name :string
body :text
timestamps
end


# --- Permissions --- #

def create_permitted?
acting_user.administrator?
end

def update_permitted?
acting_user.administrator?
end

def destroy_permitted?
acting_user.administrator?
end

def view_permitted?(field)
true
end

end


page defined in application.dryml
____________________________________________

<def tag="index-page" for="Thing">
<page merge title="#{ht
'things.index.title', :default=>['Things'] }">
<body: class="index-page thing" param/>

<content: param>
<header param="content-header">
<h2 param="heading">
<ht key="things.index.heading">
Things
</ht>
</h2>

<p param="count" if>
<ht key="things.collection.count" count="&this.size">
There <count prefix="are"/>
</ht>
</p>
</header>

<section param="content-body">
<a action="new" to="&model" param="new-link">
<ht key="things.actions.new">New Thing</ht>
</a>

<page-nav param="top-page-nav"/>

<collection param/>

<page-nav param="bottom-page-nav"/>


</section>

<section>
<card with="&@rando"/>
<view with="&@rando"/>
</section>

</content:>
</page>
</def>



hobo_hippy

unread,
Jul 23, 2011, 4:08:15 AM7/23/11
to Hobo Users
in the <card with...> i simply get a blank section formatted to the
css for a card

in the <view with...> i get (Not Available) which makes me think the
controller action I wrote isn't correct, or I'm not accessing it right

hobo_hippy

unread,
Jul 23, 2011, 4:13:11 AM7/23/11
to Hobo Users
controller correction:
___________________________________________
class ThingsController < ApplicationController
hobo_model_controller
auto_actions :all
def show
@rando = Thing.find :first, :limit => 1, :offset =>
rand(Thing.count)
end
end

and in the page i also tried

<card with="&@rando.name"/>
<view with="&@rando.name"/>

and got identical results

hobo_hippy

unread,
Jul 23, 2011, 4:25:35 AM7/23/11
to Hobo Users
WOW! ok this whole controller thing just clicked. just had to stick
the @rando... inside the def index...end and voila! totally makes
sense too.

here's to baby steps and learning this bit by bit
Reply all
Reply to author
Forward
0 new messages