RailsGrunt

a noob working the ruby railroad.

attachment_fu amazon_s3.yml

March 26th, 2007

nothing to report here but maybe someone out there got a similar careless problem like me!. Basically I was following the directions at http://clarkware.com/cgi/blosxom/2007/02/24 and everything was working fine but I ran into a problem with the loading of the amazon_s3.yml file. I tried manually setting the variables in the lib directory but no avail. finally i decided to put all the information of passkey/bucket/secret key in all three sections of the yml file. well that did the trick. amazon s3 is awesome and with attachment_fu, everything is easy!

Ported another theme to mephisto

March 20th, 2007

just realized the zip files went missing when changing domains. will update asap!

Hello, This theme was created by nodethirtythree. So far, he/she/they have been one of my favorite designers. What I like best is that it has a nice three column layout.


Read the rest of this entry

ruby rails upload multiple files using acts as attachment

March 14th, 2007

this question seems to pop up in the rails forums at least twice a week. so i guess this may be a beneficial post to folks out there looking to simply add more than 1 file field in a form. remember, the grunt is new to ror so any improvements to this post, please drop me a message.

so far this is what i did.

follow techno weenie tutorial here for creating your model that will store the photos.

In our example, lets say you have a foodrecipe model and a food_photos model. you would do “script/generate attachment_model food_photos”

Next, add your foodrecipe_id to your migration file for your foodphotos.

Next, add your relationships to your model. Therefore, foodrecipe has_many :foodphotos, and foodphotos model, belongs_to foodrecipe.

Next in the action that calls your form, instantiate as many files you would like to add. for example in the dvdcover example, you do @dvd_cover = DvdCover.new. so in our example you can do

@myrecipe = Foodrecipe.new

@myPhoto1 = Foodphoto.new

@myPhoto2 = Foodphoto.new etc…

now in your form, you can add the file fields.

for example in the dvdcover example you would do this..

<% form_for :dvd_cover, :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>

<%= f.file_field :uploaded_data %>

<%= submit_tag :Create %>

<% end -%>

but now, you can do this. (i am sure there is a much better way but this is what i know so far)

<% form_for :myrecipe , :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>

...stuff for your recipe here

<%= file_field “myPhoto1” , “uploaded_data” %>

<%= file_field “myPhoto2” , “uploaded_data” %> ...etc.

<%= submit_tag :Create %>

<% end -%>

now in your create action, build how a has_many is built by doing the following

@foodrecipe = Foodrecipe.new(params[:myrecipe])

@foodrecipe.save

@foodrecipe.foodphoto.build[params[:myPhoto1]]

@foodrecipe.foodphoto.build[params[:myPhoto2]]

that should be able to save your multiple files. this is probably the bruteforce way, so if this can be made better, please drop me a message. thanks!

ym4r gmap.header key.rb error

March 5th, 2007

just a quick post for a careless problem i had the other day when using ym4r plugin on my production server. in the gmaps_api_key.yml file in your config directory, there is the production part where it is defaulted to input different url’s. in my case, I put mywebsite.com : api_key. when i did, i was getting a funky error about GMap.rb in my view template. The problem wasn’t clear until i checked the production.log file, and noticed that the problem was reported on line 28 of the ym4r/lib key.rb file. cant remember the error but it had something to do with hash key unrecognized etc…. so i said, wait second, i needs no hash keys in my yml api so let me take it out. well, guess what, it fixed the problem. just put the key right under production definition and you should be good to go. hope this helps somebody.

to print out month, date, and day from created_at field

March 2nd, 2007

you can just do this in your view. for more information, check out the time class in the ruby api. basically if you would like to save time about a certain model/object, you can define ”:created_at, :timestamp, :null => false” in your migration file. doing this, you can print out various time formats like below.

<= yourvariable.created_at.strftime(“%b”)>

<= yourvariable.created_at.strftime(“%d”)>

<= yourvariable.created_at.strftime(“%a”)>

rails select tag with numbers

March 2nd, 2007

if you have a form and need a select list of numbers, here is a slice of code i use…

<%= select_tag :expnum, options_for_select((7..90).collect {|x| [x, x] }, :expnum) %>

So now you can access expnum in your controller view params[:expnum]