Posts Tagged ‘Ruby on Rails’

Rewrites in rails

How to do a rewrite rule in rails .htaccess to disallow rails in certain subfolders (for example, you might want to run a php forum software under /public/forum)

Mainly, you can just edit the .htaccess file in your rails app under public/.htaccess. Inside there is a short explanation and example of how to do this.

For example,

RewriteCond %{REQUEST_URI} ^/forum.*   RewriteRule .* - [L]

Helpful link Hosting rails helpful link

Optizimizing your rails app for the iphone

CSS stylesheet & viewport meta tag

Basically, you want the following in your header tag - this will give you a css stylesheet just for the iphone, and make the scale to 1.

	<%= stylesheet_link_tag 'iphone-app', :media => 'only screen and (max-device-width: 480px)' %>
	

Developing on mac with internet explorer - vmware

If you are a ruby on rails web developer like me, use a mac, and would prefer to test your rails creations in internet explorer without having to upload your app every time you make a change and then testing on a windows computer, here is how you can develop and test on windows using vmware.

First, follow this tutorial: Setting up vmware

Then, follow this tutorial: Setting up localhost through vmware

Dealing w/ main navigation in rails

Here?s a great plugin for doing navigation in rails

I didn?t like that too much.

A simpler yet elegant solution is here: simple & elegant

There is also this

And actually, what I usually do is just use the following code and then css it appropriately.

            <body id="<%= controller.controller_name %>">

Deploying with Git and Capistrano

Get your git all setup and your project gitified.

Freeze your gems and freeze to edge your project and git push that to the server (rake rails:freeze:gems and rake rails:freeze:edge or rake rails:freeze:edge TAG=rel_2-0-2 see here ) (script/about to see what version your rails is already on. maybe you already froze earlier and forgot)

Capify your project (capify .)

Edit deploy.rb file created by capistrano use this instructions also use hostingrails? wiki for capistrano two Here?s an example of deploy.rb

Edit Capfile created by capistrano example code

Set ENV[?RAILS_ENV?] ||= ?production? in config/environment.rb

Edit .htaccess file to do fcgi instead of normal cgi instructions here

On local machine run cap -T to view commands available

Add an ssh-keygen to the server as well (since it will be taking from github) see here and here

Follow the latter end of these instructions from hostingrails

Freezing rails and gems

It?s a good idea to freeze your project before deploying. To do this run the command: rake rails:freeze:gems

And it?s a good idea to freeze your rails version to by doing: rake rails:freeze:edge

Here?s a tutorial

Accessing associated table data with rails.

To access data that has related data using has_many and belongs_to, first make sure you?ve set up your related ids. For example for the two models posts and attachments, the attachments table should have a field called post_id so that the attachment can be association to the post.

Then make sure you have the appropriate has_many and belongs_to in the models

Then do the following in your view code:

                <% @posts.attachments.each do |post| %>
    <img src="<%= attachment.public_filename %>" class="attachment" /><br />
<% end%>

Email validation/activation with rails

  1. Install the restful_authentication plugin
  2. run script/generate authentication user sessions ?include-activation
  3. rake db:migrate
  4. see this link
  5. on the part where you create the mail.rb config/initializer use this link instead, pasting that code in the place of the code recommended by avnetlabs.com?s tutorial
  6. create a new file called smtp_tls.rb in your lib folder and paste this code into it
  7. open the production and development configuration files, config/environments/production.rb and config/environments/development.rb and put in the SITE_URL = ?localhost:3000? and for production your webdomain. according to avnetlabs
  8. edit the user_mailer.rb model to use the SITE_URL variable instead
  9. Open the email template files (app/views/user_mailer/activation.html.erb and app/views/user_mailer/signup_notification.html.erb) and modify as desired
  10. that?s it

Resources

Rails find with conditional dates

To use the rails find method with conditional dates do the following. inspiration from here

Example:

            Record.find :all, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Or addition: Record.sum :hours, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Now what if you want to use the current month instead of the static typed in months above?

             this_month = Date.today.strftime("%Y%m")
start_of_month = this_month + '00'
end_of_month = this_month + '31'
result = Record.sum :hours, :conditions => {:date => start_of_month..end_of_month, :user_id => current_user.id} rescue nil
result = result.round(1) rescue nil

Rails Date method:

graphs with rails

[nuby on rails article on gruff)[http://nubyonrails.com/pages/gruff]