0
votes

beginner rails question here. I created a web scraper to populate my model with information and I wanted to call this method with a button from a view. More importantly, I wanted to pass the contents of a form as a parameter to this method. I'm kinda lost on how to this using forms, but so far I have:

I placed the method into the directory app/helpers/admin_pages_helper.rb

module AdminPagesHelper
  def populate(term)
  .
  .
  end
end

I then have a view located at app/views/admin_pages/index.html.erb

I'm unsure of how to go about this using the form_tag/text_field_tag/submit_tag for a parameter; most of examples I've seen involve database queries using forms instead. Thanks

1
What is term - a single parameter as a string? - PinnyM

1 Answers

2
votes

The simplest form of usage would be something like this - firstly you need a route that your form can submit to:

# in your routes.rb
match "/admin_pages/my_action" => "admin_pages#my_action", as: "my_admin_pages_action"

This will route a request to "/admin_pages/my_action" to the "my_action" action in your AdminPagesController. It also gives you several helper methods you can use to build this url string in your views and/or controllers.

# in your view
<%= form_tag my_admin_pages_action_path do |f| %>
  <%= text_field_tag :term %>
  <%= submit_tag "Submit" %>
<% end %>

When this form is submitted it will route to the AdminPagesController#my_action method:

class AdminPagesController < ApplicationController
  def my_action
    term = params[:term]          # this contains the contents of the text_field
    something = populate(term)    # call your helper method passing in the term
  end
end