0

I've been working through "Agile Web Development with Rails" and have come across an issue.

I've been having issues with saving/retrieving data selected from a drop down menu. I have possible payment methods stored in a database table, and I have a drop down menu to select the desired payment method.

<div class="field">
    <%= f.label :payment_type_id %><br>
    <%= f.select :payment_type_id, options_from_collection_for_select(@payment_types,:id,:method), prompt: 'Select Payment Method'%>
</div>

My "create" action in the Orders controller

def create
   @order = Order.new(order_params)
   @order.add_line_items_from_cart(current_cart)
   respond_to do |format|
      if @order.save
         Cart.destroy(session[:cart_id])
         session[:cart_id] = nil
         OrderNotifier.received(@order).deliver
         format.html { redirect_to store_url, notice: 'Order has been placed' }

Everything gets saved apart from the payment_type_id foreign key, I know this because I've looked in the tables.

Any help would be greatly appreciated.

Thanks.

5
  • 1
    could you post the action rendering the form, and your complete orders_controller.rb also the ActionController::Parameters in your log after submitting the form Commented Jul 6, 2016 at 23:56
  • 1
    A good place to start is always checking your require/permit method. in this case it looks like it is named order_params - if you haven't included a field in that, then it will never be saved. If you can include your copy of that in your question, it'd help us to see if that's the case. Commented Jul 7, 2016 at 0:07
  • 1
    @TarynEast I checked my require/permit method and indeed :payment_type_id was not there. I put it in and it has solved my issue. I'm still very new to this language, thank you. Commented Jul 7, 2016 at 0:27
  • 2
    Hi @TopCat, usually you can spot that looking at rails logs, rails warns you about unpermitted parameters. Commented Jul 7, 2016 at 0:55
  • Thanks @PauloAbreu, I'll give the logs a proper look the next time I'm having issues. Commented Jul 7, 2016 at 1:13

1 Answer 1

1

A good place to start is always checking your require/permit method. In this case it looks like it is named order_params - if you haven't included a field in that, then it will never be saved.

As mentioned in another comment, you can also check if this is the case, by reading through your server logs - you will often get a message about Unpermitted params - these are the params being discarded.

Sometimes it's because the fields in the form aren't quite named correctly, so they're not coming through the way you'd expect - again looking in the server logs is a good idea as it will tell you what params are coming through and what the structure of them is and thus you have a chance of fixing them.

Here is a random example of a server log for a "widget" form and what it looks like when some of these errors occur:

Started POST "/widgets" for 127.0.0.1 at 2013-04-10 00:16:37 -0700
Processing by WidgetsController#create as HTML<br>
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"ipxBOLOjx68fwvfmsMG3FecV/q/hPqUHsluBCPN2BeU=",
 "widget"=>{"name"=>"Widget 1", "colour"=>"Blue"}, "size"=>"large"
}
Unpermitted parameters: colour

In this example: the "size" property is not correctly placed as being a parameter for a widget... also the "colour" property is being rejected. So you'd say that you need to add "colour" to the require/permit section, and move the size field so that it is properly inside the widget-form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.