ruby on rails - Using a form and a button to call a method in view -
beginner rails question here. created web scraper populate model information , wanted call method button view. more importantly, wanted pass contents of form parameter method. i'm kinda lost on how using forms, far have:
i placed method directory app/helpers/admin_pages_helper.rb
module adminpageshelper def populate(term) . . end end
i have view located @ app/views/admin_pages/index.html.erb
i'm unsure of how go using form_tag/text_field_tag/submit_tag parameter; of examples i've seen involve database queries using forms instead. thanks
the simplest form of usage - firstly need route form can submit to:
# in routes.rb match "/admin_pages/my_action" => "admin_pages#my_action", as: "my_admin_pages_action"
this route request "/admin_pages/my_action" "my_action" action in adminpagescontroller. gives several helper methods can use build url string in views and/or controllers.
# in view <%= form_tag my_admin_pages_action_path |f| %> <%= text_field_tag :term %> <%= submit_tag "submit" %> <% end %>
when form submitted route adminpagescontroller#my_action method:
class adminpagescontroller < applicationcontroller def my_action term = params[:term] # contains contents of text_field = populate(term) # call helper method passing in term end end
Comments
Post a Comment