Slicekick
Slicekick

Reputation: 2159

How do I run Ruby code on a button click?

I have a Rails partial, and a Ruby script I want to run when I click a button in the partial.

How do I do this?

This is the script I want to run - I have it in <%= %> tags right now.

 <%= require 'nokogiri'

doc = Nokogiri::HTML(table_string)

doc.xpath('//table//tr').each do |row|
  row.xpath('td').each do |cell|
    print '"', cell.text.gsub("\n", ' ').gsub('"', '\"').gsub(/(\s){2,}/m, '\1'), "\", "
  end
  print "\n"
end
%>

I want to have a button that runs that snippet when I click it.

Upvotes: 0

Views: 5605

Answers (1)

Robin
Robin

Reputation: 21884

  • Create a controller
  • Create an action that will contain the code you want to run
  • Set up your routes

Ex:

resources :my_resources do
    collection do
         get :your_action
     end
 end

And your link should be something like: link_to "text", your_action_my_resources_path.

Upvotes: 7

Related Questions