Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Application version number and cool codename based on subversion number

// description of your code here

#codename generated from the dictionary
REVISION_NUMBER = `svn info`.split("\n")[4][/\d+/].to_i
APP_CODENAME  = IO.readlines("/usr/share/dict/words")[REVISION_NUMBER]

Add Subversion Revsions Number to Rails Application.

// description of your code here
Add this to your environment.rb file for a dead easy way to get your subversion revision into your application.

APP_VERSION  = IO.popen("svn info").readlines[4]

how to escape from AJAX URL

From Dylan Stamat's post on the Ruby on Rails list


Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise. so, my " login.rjs" looks like:

if @logged_in_client
  page.replace_html "message", :partial => 'shared/bad_login'
else
  page.redirect_to "whatever you want here"
end

------ End Message--------
Here is my two cents:
Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller

render :update do |page|
  if @logged_in_client
    page.replace_html "message", :partial => 'shared/bad_login'
  else
    page.redirect_to "whatever you want here"
  end
end

Submitting data to two different tables with two different models (or how to modify multiple models from one form)

Taken from Norman Timmler's example on the ruby on rails mailing list.


Take two text fields for example:
# view
<%= form_tag :controller => :posts, :action => create %>
 <%= text_field("post", "title", "size" => 20) %>
 <%= text_field("user", "email", "size" => 20) %>
<%= end_form_tag %>

# posts_controller
class PostsController < ApplicationController
 def create
   @post = Post.create(params[:post])
   @user = User.create(params[:user])
   @post.users << @user
 end
end


In the controller the params hash has one key for every object (user,
post) in your form. As a values of this key you find a hash having a
key-value pair for every object attribute (title, email).

This way you can submit multiple objects via one form.

Create a new instance of Magick::Image out of uploaded form data

Taken from Norman Timmler's example on the Ruby on Rails mailing list:

def file=(new_file)
 new_file.rewind
 image = Magick::Image::from_blob(new_file.read).first
end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS