learningruby347
learningruby347

Reputation: 233

Using form_tag to create a button

So I have this code in my views to create a button that approves all 5 star reviews. The issue I'm facing is that button_to doesn't hide the params and order_ids is about 200 order objects. So when I click on the button, I get the error

Request-URI Too Large WEBrick::HTTPStatus::RequestURITooLarge

I think I need to use the form_tag helper but I'm not too sure how to make a button with form_tag. Or how to link the two things.

<div style="margin: 0 0 50px 0">
  <%= button_to "Approve reviews with 5 stars",
    { action: :approve_reviews, order_ids: @orders, rating: 5 }
    , method: :post %>
</div>

Upvotes: 0

Views: 349

Answers (3)

uday
uday

Reputation: 8710

You can simply add params:{} in the end to your button_to

<%= button_to "Approve reviews with 5 stars", action: :approve_reviews, params: {order_ids: @orders, rating: 5} %>

Note: by default the method will be post so you don't have to specify the method.

Hope it helps!

Upvotes: 1

Aswin Ramakrishnan
Aswin Ramakrishnan

Reputation: 3213

I'd recommend doing it this way -

  1. It is a bad idea to send so much data through the URL
  2. Have a form and a hidden field that contains the values for the order IDs (and probably Rating, unless you have your routes configured for it)
  3. Have a simple button that just submits the form when clicked

    <%= form_tag({ :action => :approve_reviews }) do -%>
        <%= hidden_field_tag :order_ids, :value => @orders %>
        <%= hidden_field_tag :rating, :value => 5 %>
        <%= submit_tag "Approve reviews with 5 stars" %>
     <% end -%>
    

Upvotes: 0

axvm
axvm

Reputation: 2113

Try something like this:

<%= form_tag({ action: :approve_reviews, rating: 5 }) do -%>
  <%= hidden_field_tag 'order_ids', @orders %>
  <%= submit_tag 'Approve reviews with 5 stars' %>
<%= end -%>

Upvotes: 0

Related Questions