SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a button from link_to_function

    Can you create a form button that performs the code shown:
    Code:
    <%= link_to_function 'Close Popup', onclick="window.opener.location.reload(true); window.close()" %>
    or a submit_tag as text vice the button.

    Rick
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to use the code from the trunk here:

    http://dev.rubyonrails.org/changeset/3367

    It contains the "button_to_function" helper that is the exact same as link_to_function except generates a button instead.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mitch,
    Is that code available just by calling it or do I copy it and add it to the application helper file?

    Rick
    Ruby, Ruby when will you be mine

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rick,

    Make a .rb file in your lib/ folder that looks like this:
    Code:
    module ActionView
      module Helpers
        module JavaScriptHelper
          def button_to_function(name, function, html_options = {})
            html_options.symbolize_keys!
            tag(:input, html_options.merge({ 
              :type => "button", :value => name, 
              :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" 
            }))
          end
        end
      end
    end
    Then in your environment.rb file, at the bottom include it by going like this:
    Code:
    require 'file_name_above'
    Where file_name_above is what you named the file with or without the .rb (if it is omitted Ruby will add it for you).

    Then you should be able to just call it in your views
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •