rss
twitter
    Find out what I'm doing, Follow Me :)

Observe_form for getting Live preview

You’d like to give your users the ability to see a live preview of their data
as they are editing it. You don’t want them to have to wait until they
submit a form. So that if there is any error it can be rectified.

Include the javascripts in the layouts.

<%= javascript_include_tag "default" %>

Now that we’ve got the necessary JavaScript libraries loaded, we’ll create
the model and controller.Suppose the controller name is Content and model name is Example. There are two fields in the table ('title' and 'body' )

Now in the model define the attribute accessor

class Example
attr_accessor :title, :body
end


We will create a new method

def new
@data= Example.new
end



Create the file, app/views/content/new.rhtml, and edit it to look like
the following.

<% form_for(:example,:url => {:action=>'create',:html => {:class=>'wufoo', :id => "form"} ) do |form| %>
<%= text_field :example, :title %>

<%= text_area :example, :body %>

<%= submit_tag "Save" %>
<% end %>
<%= observe_form "form",
:frequency => 5,
:update => "preview",
:complete => "Element.show(' preview' )",
:url => { :action => "preview" } %>


In the method preview we write

def preview
render :layout => false
end



The only job of the action code is to short circuit the application’s usual
rendering. Since we’re going to be updating the live-preview element of
our diary entry creation page with the full results of the preview( ) action,
we don’t want it returning a full HTML page.

The preview action’s view, in app/views/content/preview.rhtml should look
like this:

Form Preview


<%= params[:example][:title] %>


<%= textilize params[:example][:body] %>


That’s all there is to it! This view prints the entry’s title as an HTML
heading and then generates HTML output via the textilize( ) method. This
method uses the RedCloth library internally to transform very simple
text markup to HTML.

Ajax Editing using In-Place Form Editing

Your application has data that are often edited by
your users—usually very quickly. You want to give your users an easy
way to edit application data in place without having to open a separate
editing form.

Rails makes in-place editing easy with the script.aculo.us InPlaceEditor
control and accompanying helpers.

First create a model and controller.

Put JavaScript files in your views. Somewhere in the head
of your HTML document, you can call the following:

<%=javascript_include_tag :defaults%>

put the in place editor in you .rhtml file like this


<%=in_place_editor_field :contact, :name%>


In Controller write the following line
in_place_edit_for :contact, :name

and its done.

Ajax Pagination in rails

Ajax Pagination in Rails is very simple and DRY. You Just have to put ajax helper method in applicartion .rb. And its done. Here is the procedure.:

1) Put the following line of code in apllication_helper.rb file.

def ajax_pagination_links(paginator, options={})

options.merge!(ActionView::Helpers::PaginationHelper::DEFAULT_OPTIONS) {key, old, new old}
window_pages = paginator.current.window(options[:window_size]).pages
return if window_pages.length <= 1 unless options[:link_to_current_page] first, last = paginator.first, paginator.last returning html = '' do if options[:always_show_anchors] and not window_pages[0].first? html << update =""> options[:update], :url => { options[:name] => first }.update(options[:params] ))
html << ' ... ' if window_pages[0].number - first.number > 1
html << ' ' end window_pages.each do page if paginator.current == page && !options[:link_to_current_page] html << update =""> options[:update], :url => { options[:name] => page }.update(options[:params] ))
end
html << ' ' end if options[:always_show_anchors] && !window_pages.last.last? html << ' ... ' if last.number - window_pages[-1].number > 1
html << update =""> options[:update], :url => { options[:name] => last }.update( options[:params]))
end
end
end



2) Now in the controller in you method put the following code.

def user
@content_pages, @contentdata = paginate :users, :order =>"create_date DESC", :per_page => 6
if params[:page]
render :update do page
page[:center].replace_html :partial=>'user'
end
else
render :action=>'user'
end
end



3) And in the last just put these lines in the view part (rhtml) and its done.



<%=ajax_pagination_links @content_pages, {:params => {:search_query => @params[:search_query]} }%>