steel
steel

Reputation: 12520

Rails: Form Routes to Wrong View

I have a form submitting to a custom action, 'add_participants', on the 'order_items' controller. It appears to submit to the correct action, but the error references an unrelated view, the view associated with 'order_items#new'.

What I have tried includes:

The Error

ArgumentError in OrderItems#add_participants

Showing /path/to/app/views/order_items/new.html.erb where line #7 raised:

First argument in form cannot contain nil or be empty

Extracted source (around line #7):

<%= form_for @order_item do |f| %>
<div class="field">
<%= f.hidden_field :company_id, :value => '2' %>

app/views/order_items/new.html.erb:7:in `_app_views_order_items_new_html_erb__1443667659032443898_70275798467060'
app/controllers/order_items_controller.rb:78:in `block (2 levels) in add_participants'
app/controllers/order_items_controller.rb:71:in `add_participants'

The Route

post '/add_participants', to: 'order_items#add_participants'

The Submitting Form

<%= form_for @new_oi, :url => {:action => "add_participants"}, method: :post, :html => { :id => "add_participants", :class => "add_participants" } do |f| %>

Controller Action

*line 71*       def add_participants
        @original_oi = OrderItem.find_by_id(session[:current_order_item_id].to_i)
        @order = @original_oi.order
        @new_oi = OrderItem.new(order_item_params)

        respond_to do |format|
            if @new_oi.save
                format.html { redirect_to change_confirmation_path }
                format.json { render action: 'show', status: :created, location: @new_oi }
            else
*line 78*           format.html { render action: 'new', notice: "We're sorry, but we were unable to complete this purchase." }
                format.json { render json: @new_oi.errors, status: :unprocessable_entity }
            end
        end
    end

Thanks in advance.

Upvotes: 0

Views: 90

Answers (1)

steel
steel

Reputation: 12520

Figured it out. I just needed to study the error location more closely. The record wasn't saving, so the add_participants action was attempting to render the new view.

Upvotes: 1

Related Questions