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

Oliver Haag www.ohcon.de

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

convert between characters and values

// character to ASCII value: use ?
?a     # => 97
?\n    # => 10


// string to integer: use []
'a'[0]        # => 97
'hallo'[1]    # => 97


// integer / number to character: use .chr
97.chr     # => "a"
10.chr     # => "\n"


//more info: "Ruby Cookbook", O'Reilly

radio button, boolean selection

// radio buttons generated with form helper
// selection by boolean
// attention interger numbers (0, 1) don't work
    <div class="row">
      <dl>
        <dt>Owner over 18?</dt>
        <dd>
        <%= radio_button( :guarantor, :over_eighteen.to_s, true )%> Yes &nbsp;&nbsp;
        <%= radio_button( :guarantor, :over_eighteen, false )%> No
        </dd>
      </dl>
    </div>

create object fom underscore_syntax

// camelize - from underscore_syntax to Uppercas/ClassSyntax
// constantize - create instance
// (params[:property]) -- read data from web-form
    @property = property_type.camelize.constantize.new(params[:property])

transform normal projet to rails project in radrails

// in radrails when you import a project from subversion
// it is a normal project

// in the menu I found no way to transform it
// but so it worked for me:

// 1. edit the .project file and copy the natueres block

	<natures>
		<nature>org.radrails.rails.ui.railsnature</nature>
		<nature>org.rubypeople.rdt.core.rubynature</nature>
	</natures>


// 2. copy the buildSpec block too
// (I'm not sure if this is necessary, I did both and it worked)

	<buildSpec>
		<buildCommand>
			<name>org.rubypeople.rdt.core.rubybuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>


// 3. restart radrails and
// 4. add a webrick-server to the project
// and you are done

find out the current url / uri in *.rhtml file

// find out the current url / uri in *.rhtml file
// is quite simple with the request object

<% page = request.request_uri %>
page: <%= page %>

// but then different urls mean the same page
// (../admin = ../admin/ = ../admin/index = ..admin/index/)
// and maybe the id is unwanted too (../admin/show/8)
// so below is an alternative with control on which parameter is used

<% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
page: <%= page %>

rails generator syntax

// generate application
rails projectname


// generate migration.
ruby script/generate migration migration_name
ruby script/generate migration add_price


// generate model
// we can give a list of columns and types
// :string, :text, :integer, :decimal, :float, :date, ...
ruby script/generate model model_name
ruby script/generate model user name:string hashed_password:string salt:string


// generate controller
ruby script/generate controller controller_name method_name(s)
ruby script/generate controller store index


// generate scaffold
ruby script/generate scaffold model_name controller_name
ruby script/generate scaffold product admin
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS