Hi! Welcome...

Kolodvor Vlado Cingel is Rails developer from Croatia. Apart from Ruby & Rails, I really enjoy scripting in JavaScript and converting designs into great HTML & CSS.

02 January 2010 ~ 1 Comment

Set focus on first field with Prototype

If you are using jQuery see this post.

To set focus on first text field with Prototype I prefer something like this

  var firstField = $$('input:text:visible').first();
  if (firstField)
    firstField.focus();

but you can also try with Form.focusFirstElement or Form.findFirstElement

02 January 2010 ~ 0 Comments

Rails, CSRF and Ajax requests

Rails protects controller actions from CSRF (Cross-Site Request Forgery) attacks with a token based on a random string stored in the session. The token parameter is named authenticity_token by default and will be embedded in all forms and Ajax requests generated by Rails.

You should also add this token to all Ajax request that you hand coded. As suggested in Rails documentation you can add this line in head section.

  <%= javascript_tag "window._token = '#{form_authenticity_token}'" %>;

and then add authenticity_token to parameters option of Ajax requests

  new Ajax.Request('/some/url', {
    parameters: "foo=bar&authenticity_token="+_token
  });

Remote forgery protection plugin

This can get tedious if you have a lot of Ajax requests so I wrote a simple plugin that adds authenticity token to all Ajax requests automatically.

You can install it with

  script/plugin install git://github.com/vlado/remote_forgery_protection.git

Now all you have to do is add this line inside head section of you’re layout

  <%= remote_forgery_protection %>

and all non GET Ajax request will have authenticity_token parameter automatically included.

[...]

19 September 2009 ~ 0 Comments

Radiant CMS, disable caching in development

I am working on extension for Radiant CMS. I wanted to disable caching for development, but wasn’t able to find right method for this, so finally I did it by putting this into my extensions activate method

if RAILS_ENV=="development"
  Page.class_eval {
    def cache?
      false
    end
  }
end

This is more of a hack then a real solution, so please use comments to point me in the right direction. I’m using Radiant version 0.8.1.

16 September 2009 ~ 0 Comments

Stripe table rows with Prototype

I’ve spend a lot of time with Prototype last few days, and this is going to be my first post related to prototype. So let’s see how to stripe the table using prototype, I think you will be surprised how easy it is.

HTML for our table will look like

[...]

17 January 2008 ~ 7 Comments

Set focus on first field with jQuery

f you are using Prototype see this post.

Setting focus on the first text field with jQuery is as simple as

  $("input:text:first").focus();
  $("input:text:visible:first").focus();

Find more at http://docs.jquery.com/Selectors

Update: If you are on Rails use auto_focusable_forms, dependency free plugin that will focus first input for you.

08 December 2007 ~ 3 Comments

Blur links with jQuery

While surfing the Web, you probably noticed the dotted outline, that appears when you click on the link. If your link doesn’t lead to another page, but instead triggers some event in the same page, outline stays there and looks ugly.

For modern browsers you can remove it with simple css:

a:focus, a:active {
  outline:none;
}

Unfortunately, this won’t do for IE 6 and earlier.

With jQuery it’s easy to get rid of the outline in every browser that has JavaScript enabled:

$("a").click(function() {
  $(this).blur();
});

This will remove outline from the link once he is clicked. You will see the outline when clicking, but it will not stay there.

If you don’t want to se the outline at all replace click event with focus event:

$("a").focus(function() {
  $(this).blur();
});

To find more about jQuery Events visit http://docs.jquery.com/Events