jeudi 1 décembre 2016

Rails - How update data in table when user clicks on submit button

I am using Rails 3.2 and have this page where I am showing a table with a list of results, passed to the view through a @jobs variable which are results from a SQL query, and I want to add a button to this page so that when the user clicks on it, a controller action is called, which does some DB work, then refreshes the table with the up to date results.

I am completely new to JS and AJAX, and pretty green with Rails so I have no clue how to do this.

I create a button like this:

<%= form_tag( "controller/get_job", class: "text-center", id: "get-job-form" ) do %>
<%= submit_tag "Get Job", id:"get-job-submit", class: "button" %>
<% end %>

thinking that it would call the get_job action of my controller. But I don't want this action to lead to a new page, I just want to "refresh" that table inside the page the user is already on.

My controller action is something like:

def get_job
  job = find_job
  job.employee = me
  job.save
  # Here I want to refresh the table so that this new job shows up in   the table
end

Do I need some javascript script to do this or is it doable just by calling the controller action and then maybe calling render again?

I am pretty lost, any help is appreciated! Thank you.

Aucun commentaire:

Enregistrer un commentaire