26 Nov 2009 @ 2:59 PM 

Interesting post here ( http://kpumuk.info/ruby-on-rails/simplifying-your-ruby-on-rails-code/ ) about Presenter patterns but buried in it is a little snippet of code that I want to store away for later

“I prefer to put presenters into a separate folder app/presenters, so first we should add it to Rails load path. Add this line to your config/environment.rb:”
config.load_paths += %w(#{Rails.root}/app/presenters )

There are classes that I’ve got in app/models/ that I don’t feel should be there as they don’t relate to tables in the database, I can use this to arrange them in a more organised manner


Tags Categories: Rails Posted By: admin
Last Edit: 26 Nov 2009 @ 02 59 PM

E-mailPermalinkComments (0)
 26 Jun 2009 @ 11:50 AM 

This has tripped me up twice trying to connect a new install to the oracle db I’m using. The error message suggests that you run ‘get install activerecord-oracle-adapter’, however that will come back and tell you that it couldn’t be found locally or in the repository.

You need to run:

gem install ruby-oci8 -v 1.0.6  (this is the current stable version at time of writing)
gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org
Tags Categories: Rails Posted By: admin
Last Edit: 26 Jun 2009 @ 01 58 PM

E-mailPermalinkComments (0)
 11 Mar 2009 @ 1:30 PM 

There are a number of gotchas when using rails with legacy db’s. Here are the ones I’ve ran headfirst into and how I got round them

Table name doesn’t follow rails naming conventions:
set_table_name ‘legacy_name’

Primary key doesn’t follow rails naming conventions:
set_primary_key ‘legacy_primary_key_name’

Using a sequence (need to do this in oracle):
set_sequence_name ‘legacy_sequence_name’

So far so straightforward, you might however also run into problems with some field names.

Table.type:
set_inheritance_column ‘not_type’
ActiveRecord inheritance works automagically comes into play if you have a column named type, by setting it to not type it won’t try to use pixiedust on your model.

Table.class:
class << self
def instance_method_already_implemented?(method_name)
return true if method_name == ‘class’
super
end
end

def pclass=(text)
self[:class] = text
end
def pclass
self[:class]
end

You might find some of your field names clashing with some basic ruby / rails keywords the first section will prevent your field being hidden and the pclass attr allows you to rename the field name in your model.

If I come across any other I’ll update this list but it’s handy stuff to know.

Tags Tags: , ,
Categories: Rails, Ruby
Posted By: admin
Last Edit: 11 Mar 2009 @ 01 30 PM

E-mailPermalinkComments (0)
 11 Dec 2008 @ 10:16 PM 

UPDATE: Hands up I made a boo-boo, I had listed the variable that holds the token as ‘form_authentication_token’ and it should have been ‘form_authenticity_token’, many apologies for the inconvenience.

So I was trying my first ajax calls to a rails backend and I hit this ugly error -

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)

I hunted about on google till I found the answer. The problem is that Rails automagically adds a randomly generated token ( bunch of characters ) that it inserts into any forms created with any of the form helpers (form_for, form_tag etc).  When the form is posted Rails checks the token is valid before processing the request.

This code

<h1>New productsetupinfo</h1>
<% form_for(@productsetupinfo) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :setup %><br />
<%= f.text_area(:setup, :cols=>"80",:rows=>"30") %>
</p>
<p>
<%= f.label :note %><br />
<%= f.text_area(:note, :cols=> "64", :rows=>"1") %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', productsetupinfos_path %>

Produces this html

<form action="/productsetupinfos" class="new_productsetupinfo" id="new_productsetupinfo" method="post">
<div style="margin: 0pt; padding: 0pt;">
<<< Authenticity Token automagically added here >>>
<input name="authenticity_token" value="7054c89779d7c1b6ed64e5261b49de76d1539974" type="hidden">
</div>

<p>
<label for="productsetupinfo_setup">Setup</label><br>
<textarea cols="80" id="productsetupinfo_setup" name="productsetupinfo[setup]" rows="30"></textarea>
</p>
....<snip>

The problem with making an ajax call is that Rails doesn’t have the opperchancity to insert the authenticity token, so it’s up to you to do it manually. Fortunately you can access the authenticity token in your views using form_authenticity_token. You only need to make 2 changes to your code to get it working.

To test this we’ll create a page that doesn’t work and then fix it so it does.

First : Create a new Controller. From the root of your rails app

script/generate controller Authfix index js_responder

Next : Create methods in new controller

class  AuthfixController < ApplicationController

def index
end

def js_responder
respond_to do |wants|
wants.js { render :text => 'AJAX successful - Barman! Lard for everyone!' }
end
end

end

And finally create the view

<html>
<head>
<<< You may need to change this to point to your jquery library >>>
<%= javascript_include_tag "jquery">
<script type="text/javascript">
$(document).ready(function()
{
$('#makecall').click(function()
{
$.post('/authfix/js_responder',{ somedata:'somedata' },function(data,status){ $("#response").html(data) },'text');
}
});
</script>
</head>
<body>
<input type="button" id="makecall" value="Press me" /><br />
<div id="response">Nowt yet</div>
</body>
</html>

Now run this and click the button and nothing will happen, if you check your logs you will see our friend the InvalidAuthenticityToken error rearing its head.

Btw, interestingly if you make an Ajax call and pass no data in the 2nd $.post argument it works fine.

To fix the problem you need to store the auth token in a js variable (if available) and automatically add it to any ajax calls

class  AuthfixController < ApplicationController

def index
end

def js_responder
respond_to do |wants|
wants.js { render :text => 'AJAX successful - Barman! Lard for everyone!' }
end
end

end

And finally create the view

<html>
<head>
<<< You may need to change this to point to your jquery library >>>
<%= javascript_include_tag "jquery">
<script type="text/javascript">
$(document).ready(function()
{
<<< Store Authentication Token here >>>
var AUTH_TOKEN = "<%= protect_against_forgery? ? form_authenticity_token : '' %>";
<<< Automatically add token to ajax calls >>>
$.ajaxSetup({data:{authenticity_token : AUTH_TOKEN}});

$('#makecall').click(function()
{
$.post('/authfix/js_responder',{ somedata:'somedata' },function(data,status){ $("#response").html(data) },'text');
}
});
</script>
</head>
<body>
<input type="button" id="makecall" value="Press me" /><br />
<div id="response">Nowt yet</div>
</body>
</html>

Add the two lines above and re-run the app and you should now get a response after clicking the button.

Result.

Tags Tags:
Categories: Jquery, Rails
Posted By: admin
Last Edit: 11 Feb 2009 @ 11 30 AM

E-mailPermalinkComments (0)
 10 Dec 2008 @ 2:00 PM 

Howdy,

This blog is going to be a handy dumping group for all the web-dev stuff I learn, leaning heavily on Ruby, Rails and Jquery – a web development triumvirate that I’m a big fan of.  I’ve got a big project at work that is using all three and I’ll be learning as I go along but I’m very much in Get-It-Done mode at the moment which means that I’m pretty much hacking out a working system just now without worrying about ( or being aware of ) half the nice stuff that Ruby/Rails/Jquery provides.

I’m noticing that while I’m knocking together this app that there are a lot of areas of Rails in particular that I’m currently glossing over and I’m not too keen to complicate the job by adding in new automagical functionality without a clear idea of what it does or how it works. My plan is to explore these areas and write up small test projects that clearly show the how, why, where and what’s of a certain section that I’m unfamiliar with.

Hopefully this will build out to be a useful resource for people like myself, starting out in R/R/J and looking for some examples as well as being a handy brain dump for myself.

If anyone has any requests, questions, (most likely) have spotted some gross errors in my code or simply want to rant and shout then fire away.

Cheers

Tags Tags:
Categories: Eee-PC, General, Jquery, Linux, Rails, Ruby, Uncategorized
Posted By: admin
Last Edit: 07 Jul 2009 @ 02 32 PM

E-mailPermalinkComments (0)
\/ More Options ...
Change Theme...
  • Role »
  • Posts »
  • Comments »
Change Theme...
  • VoidVoid (Default)
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LiteLightweight