0

I am working on a sign up application for presentations in Ruby on Rails. As such, I am displaying a list of available presentations, with the last column displaying a button to register for that specific presentation.

<table class="table">
  <thead class="thead-dark">
    <tr>
      <th scope="col">Name</th>
      # ...
    </tr>
  </thead>
  <tbody>
    <tr>
    <% @presentations.each do |pres| %>
      <td scope="row"><%= pres.Name %></td>
      # ...
      <td scope="row">
        <% unless @current_student.Selected == pres.Titel %>
        <%= form_tag students_select_path do %>
          <%= submit_tag "Choose", class: "btn", value: pres.Title %>
        <% end %>
      </td>
    </tr>
  </tbody>
</table>

I would like the button to say "Choose", then send off the parameter pres.Title to a function that I have defined. For some reason, the button in the table shows the value of pres.Title, and not of "Choose". How can this be fixed?

Current working implementation:

def addtodb
  @student = Student.find_by(id: session[:student_id])
  if @student.Selected == nil
    prestitle = params[:commit]
    @schueler.update_attribute(:Selected, prestitle)
    redirect_to students_select_path
  end
end
  • I would go with a JavaScript solution instead. Forms are not supposed to go inside of table rows. You can read more about it here: https://stackoverflow.com/questions/5967564/form-inside-a-table/5967613 – Cannon Moyer Sep 04 '18 at 15:31

1 Answers1

0

I would like the button to say "Choose", then send off the parameter pres.Title to a function that I have defined. For some reason, the button in the table shows the value of pres.Title, and not of "Choose"

submit_tag(value = "Save changes", options = {}) public

<%= submit_tag "Choose", class: "btn", value: pres.Title %>

does not generates the submit button with text Choose because you are overriding the value as pres.Title. You need to change it to just

<%= submit_tag "Choose", class: "btn" %>

And you can use hidden_field_tag instead for that purpose like so

<%= form_tag students_select_path do %>
  <%= hidden_field_tag 'prestitle', pres.Title %>
  <%= submit_tag "Choose", class: "btn" %>
<% end %>

Finally, access the value with params[:prestitle] in the controller.

Note: As one of the Rails conventions, the attribute names should be of lowercase. I recommend you to follow it in your app.

Community
  • 1
  • 1
Pavan
  • 32,201
  • 7
  • 42
  • 69