English 中文(简体)
How to use Scriptaculous effects on error messages
原标题:

I want to use some Scriptaculous effects on error messages.

<% form_for(@page) do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name %>
    <%= f.error_message_on "name" %>

    <%= f.label :content %>
    <%= f.text_field :content %>
    <%= f.error_message_on "content" %>

    <%= f.submit  Create  %>
<% end %>

My first idea is to put error messages into div tags and use page.visual_effect in the controller. But I don t know how to choose correct divs that will be affected.

<% form_for(@page) do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name %>
    <div id="errorname"><%= f.error_message_on "name" %></div>

    <%= f.label :content %>
    <%= f.text_field :content %>
    <div id="errorcontent"><%= f.error_message_on "content" %></div>

    <%= f.submit  Create  %>
<% end %>

Or should I put some if conditions into view and call them from there. By the way I don t know how to that. Can t we do something like f.error_message_on "name", :visual_effect => ... Any help will be appreciated.

最佳回答

You are on the right track with wrapping in divs, but you can do it this way in the view:

<%= f.error_message_on "name",
      :css_class => "inputError" %>

There are probably many ways to do it. Having the controller return rjs is one way. To be unobtrusive, considered best practice, you would need to include a javascript file that fires the scriptaculous method when the page is loaded. I use the low pro library for unobtrusive javascript. Here is my suggestion:

layout file:

<%= javascript_include_tag :defaults, ‘lowpro’,  form_behaviors.js  %>

javascript_file_for_form_actions.js:

Event.addBehavior({ 
   .inputError  : function() {
    this.hide();
    this.blindUp();
  }
});

You could also conditionally load the javascript file using content_for and change your layout file.

View file:

<% content_for(:javascript) do %> <%= javascript_include_tag “form_behaviors” %>
<% end %>

Somewhere in Layout file:

<% yield :javascript %>

You can get a unobtrusive plugin that includes lowpro for you as well. For more information Peepcode has a good pdf about lowpro, as well as some good screencasts on javascript.

问题回答

Just put your error message in an identifiable element such as:

<div id="bob">error messages here</div>

Then in your javascript you could do something like:

$( bob ).blindUp();




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签