Asan
Asan

Reputation: 151

Rails: Select tag causing param missing error

I have an Order model associated with Order Statuses. In the edit view I have a form_for with order status options on a select tag. When the view is rendered I can see the current order status pre-selected in the select tag. If I change it and submit the form I get this error: ActionController::ParameterMissing in OrdersController#update When I replace the select tag with a text_field I can change the order status id and submit the form successfully. I tried adding this to the form: :method => :put but gives me a routing error.

I researched my problem but I can't get it to work. Links I've researched:

rails 4 form_for with f.select isn't passing correct params, getting `param is missing` error

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Rails ActionController::ParameterMissing in QuestionsController#update

Order Edit view:

<%= form_for(@order)  do |f| %>
              <% if @order.errors.any? %>
                <div id="error_explanation">
                  <h2><%= pluralize(@order.errors.count, "error") %> prohibited this contact from being saved:</h2>

                  <ul>
                  <% @order.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                  <% end %>
                  </ul>
                </div>
              <% end %>
              <div class="form-group">
                <h3>Order Id <%= @order.id %> | <%= @order.uuid %></h3> 
                <h3>Order Total: <%= number_to_currency(@order.total) %></h3>
              </div>
              <br>
              <div class="form-group">
                <%= f.label :order_status_id %><br>
                    <%= select_tag :order_status_id, options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %> 


              </div> 

              <div class="actions">
                <%= f.submit "submit", :method => :put, :class => "btn btn-primary" %>
              </div>
            <% end %> <!-- Order details -->

orders_controller.rb

  def edit
    authorize! :read, @orders
    @order = Order.find_by_uuid(params[:id])
    @reservations = @order.reservations

  end

  def update
    @order = Order.find_by_uuid(params[:id])
    @order_status = @order.order_status_id
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { render :show, status: :ok, location: @order }
      else
        format.html { render :edit }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end
def order_params
   params.require(:order).permit(:subtotal, :tax, :total, :order_status_id, :uuid, :id, :shipping)

end

order.rb belongs_to :order_status

order_status.rb has_many :orders

Error:

param is missing or the value is empty: order

Rails.root: C:/Users/Antonio/Desktop/RegiSportV01/regisports
Application Trace | Framework Trace | Full Trace

app/controllers/orders_controller.rb:114:in `order_params'
app/controllers/orders_controller.rb:86:in `block in update'
app/controllers/orders_controller.rb:85:in `update'

Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"DhUNbbAz/VBLoRmLN8sKf4Oq/SEWJt38HlWMQW/w+zjM3CZ0Sml4Ulv+dzwYDdv1fMN42sjF+Y4BcMwc5+sRyg==",
 "order_status_id"=>"2",
 "commit"=>"submit",
 "id"=>"ebce4e1c-d81c-4171-aafa-a9e23b392525"}

Upvotes: 1

Views: 1878

Answers (2)

davidwessman
davidwessman

Reputation: 1228

I did struggle with this before as well.

Since I started using SimpleForm I have not had these issues.

It does take some setup, but your forms become far more readable and cleaner.

This field would look like:

<%= f.input :order_status_id, collection: OrderStatus.all %>

And for select fields like this I really recommend using some Javascript to make it easier for users. I have had great experience with a library called Select2 which can be included with this gem.

Upvotes: 0

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

I suggest you to change the select_tag helper:

 <%= f.select_tag :order_status_id, options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %> 
         ^^^^^^^^^^

This trick should include your order_status_id to the params with a key order.

Or you can change a name for select_tag helper.

<%= select_tag 'order[order_status_id]', options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %> 

Read the documentation for Form Helpers you can save yourself a lot of time

Upvotes: 2

Related Questions