0

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.

3
  • If you want a button to run ruby code (which must be done on the server) it sounds like you want to tie that button to an ajax request which leads to a controller action that will run the code, correct? Commented Jul 13, 2012 at 19:47
  • 2
    As often as I see questions like this, I felt prompted to write a blog entry for it. Does this help any? dgrmm.wordpress.com/2012/07/13/web-application-101 Commented Jul 13, 2012 at 22:03
  • That helps a lot! I'm 17 and I've taught myself everything I know about programming so posts like these help me learn a lot. Commented Jul 14, 2012 at 16:15

1 Answer 1

7
  • 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.

Sign up to request clarification or add additional context in comments.

4 Comments

So I have to create a controller? There's no way to just run it from a partial?
Well, if you put this code in your partial, it will run, but when the partial is rendered, not when the user will click the button. You could do it in javascript otherwise.
@Slicekick: No there's not. It's M-V-C. Controller decides what to do upon any request. :)
Ok, I understand! I will accept your answer when I get it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.