0
votes

I cannot explain why, but somehow my form_tag form is skipping streight to my view and completely bypassing my controller action.

This is my form_tag:

<%=form_tag url_for(:controller=>"orders", :action=>"finalize_order"), :method=>'post' do%>
<%=hidden_field_tag "cc_id_selection"%>
<%=hidden_field_tag "address_id_selection"%>   
<%=submit_tag "Checkout", :class=>"btn btn-primary"%>
<%end%>

And here is my controller action:

def finalize_order
  @selected_user_card_id = UserCard.find(params[:cc_id_selection])
  begin #in case they chose pickup
    @selected_address = Address.find(params[:address_id_selection])
  rescue
    @selected_address = params[:address_id_selection] #we can add options besides pickup if we'd like
  end
end

This is what is logged in my console (with some ip stuff taken out) I tried putting a trace in the controller and don't see any sign of it:

Started POST "/finalize_order"  Processing by OrdersController#finalize_order as HTML

I see at the bottom of the error page that the two params were successfully posted. Yet when I submit I get error in the next view indicating no param was instantiated in the controller. Even stranger, when I comment out the action I get the same exact results! I even tried using a route, anf get the same results. It seems I am completely skipping the action and going streight to the view. What might cause this bug?

2
Please post your routes file, and how do you know that your code bypasses the controller method. Have you inserted any puts statements in that method? - Arslan Ali
Other thing: You aren't passing anything in cc_id_selection and address_id_select. - Arslan Ali
I know that I am bypassing the action because when I comment it out nothing scary happens (ERROR: no action, etc). - Ester Lin
If the action isn't there, it'll just open the view. What do the logs show? Are you able to find a UserCard in the db? - Mark Swardstrom
As far as the values, those are inputted with javascript which I didn't include because it is not relevant to the question. I know the params are being passed because they're displayed at the bottom of the error page, as noted in the question. - Ester Lin

2 Answers

0
votes

I suspect you've accidentally made your finalize_order method private in your controller. If it's a private method, it won't be called, and the view will be rendered directly. This is a little-known feature of Rails - the action doesn't actually have to exist.

If the method is private, (the private keyword will be above it in the controller somewhere) then move the action to above the keyword to make it public.

0
votes

Along the lines of what sevenseacat suggested, but the actual problem is that I accidentally had two finalize_order methods. I guess rails couldn't decide which one to run, so it ran neither.