Iggy
Iggy

Reputation: 5251

How to create dropdown with rails form using bootstrap?

The form tag below works. However, I wanted to add styling and substitute text_field_tag into dropdown select so user can select the hours.

<%= form_tag("/availability", method: "post") do %>
  <%= label_tag(:available_hour, "Add Availability:") %>
  <%= text_field_tag(:available_hour) %>
  <%= submit_tag("Add") %>
<% end %>

I am having trouble creating form with dropdown select using bootstrap form. I tried:

<form method="post" action="/availability">
  <select class="custom-select">
    <% (1..24).to_a.each do |el| %>
      <option value=el><%= el%></option>
    <% end %>
  </select>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

But it gave me ActionController::InvalidAuthenticityToken error.

This is done inside user show view, and the action I am firing is from Availability model' create action.

I found a relevant SO post on bootstrap form, but the answer uses PHP. This is what he used: <form action="results.php" method="POST" role="form" class="form-horizontal">.

I think if I can somehow point to bootstrap to look for Availability, it should work.

How can I point out to bootstrap form which controller (and method) to use?

Upvotes: 0

Views: 604

Answers (2)

widjajayd
widjajayd

Reputation: 6253

form_tag helper link, you can add option , authenticity_token: false

actually rails already has actionview helper to help you create dropdown with hour here is link

for your problem you can try

<%= form_tag("/availability", method: "post", authenticity_token: false) do %>
  <div class="row form-group">
    <%= f.label "Add Availability:", :class => 'control-label col-sm-3' %>
    <div class="col-sm-5">
      <%= select_hour :available_hour, :class => 'form-control') %>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
<% end %>

Upvotes: 1

Bricky
Bricky

Reputation: 2745

You're probably omitting a CSRF value that's automatically included by form_tag.

Try adding

#app/views/layouts/application.html.erb
<%= csrf_meta_tags %>

Or you can add it to the form block directly:

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>

Upvotes: 1

Related Questions