Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Wednesday, April 02, 2008

Rails is moving over to Github

It looks like keeping up with edge Rails for Windows' users will get harder in the very near future. Here is the post announcing the move from using SVN to Git. I look into using Git on Windows before but it doesn't work well on Windows. It works if you run it on top of cygwin. Otherwise, it is a no go for now.

Monday, February 04, 2008

form_remote_tag with ajax and none ajax action

Does anyone else having the same problem as I do when trying to set the url for AJAX and none AJAX actions for the form_remote_tag? I always seem to forget how to do this. It always take me a couple of tries to get this right. Anyway, I am putting the correct way to do this here so I come back later when I forget.

<% form_remote_tag :url => {:controller => '/posts', :action => 'view'},
:html => {:action => {:controller => '/posts', :action => 'view', :id => @id}} do %>
<%= submit_tag 'View' -%>
<% end %>

<form action="/posts/view/1" method="post"
onsubmit="new Ajax.Request('/posts/view/1',
{asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
<input type="submit" value="View" />
</form>

Friday, January 04, 2008

adding special helpers

I was cleaning up my view and decided that I want to add two helpers, display_if and display_unless to help clean up the view.

Instead of doing:

<% if @login %>
Hello <%= @user_name -%>.
<% end %>

It would be like this:

<% display_if @login %>
Hello <%= @user_name -%>.
<% end %>

I came from the Asp.Net world so writing a custom control to do this is not easy. I was expecting it to be difficult in Rails also but it turned out to be very simple.

It took me less than 10 minutes to figure it out from not knowing how to do it at all. Hopefully, I am doing it correctly.
Here is the code for the two helpers. Just put them in your application_helper.rb file.

def display_if(condition, &block)
raise ArgumentError, "Missing block" unless block_given?

if condition
output = capture(&block)
concat(output, block.binding)
end
end
def display_unless(condition, &block)
raise ArgumentError, "Missing block" unless block_given?

unless condition
output = capture(&block)
concat(output, block.binding)
end
end

Friday, December 14, 2007

added method to help create and drop foreign key

We have a naming convention at my work place for foreign key so I made two methods to help in the creation and dropping of foreign key. I made them because I got tired of typing out the query everytime.

We name our foreign key like this: fk_table_column_reference_table_column. So a foreign key from a "users" table to the "employers" table would look like this. fk_users_employer_id.

This is how you call the method to create a foreign key.


create_foreign_key(:users, :employer_id, :employers, :id)
drop_foreign_key(:users, :employer_id)



def create_foreign_key(table, column, foreign_table, foreign_column)
execute "ALTER TABLE #{table.to_s} ADD CONSTRAINT fk_#{table.to_s}_#{column.to_s} FOREIGN KEY (#{column.to_s}) REFERENCES #{foreign_table}(#{foreign_column})"
end

def drop_foreign_key(table, column)
execute "ALTER TABLE #{table.to_s} DROP FOREIGN KEY fk_#{table.to_s}_#{column.to_s}"
end

Friday, December 07, 2007

Rails 2.0.1 is out

I am sure this is all over the web but Rails 2.0.1 is out and here is the announcement. I probably won't use it for my current project until after we do a release.