2

I keep getting an

undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>

when navigating to my new order path url /users/1/orders/new

Been stuck on this, not sure what the deal is.

Routes:

devise_for :users, :path => 'accounts'

  resources :users, only: [:index, :show] do
      resources :orders
  end

root index:

<%= link_to "Create an Order", new_user_order_path(current_user.id) %>

form:

<%= form_for([@user, @order]) do |f| %>

<%= f.hidden_field :user_id %>

  <div class="form-group">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true, class: "form-control" %>
  </div>

<% end %>

new:

<h1>New Order</h1>

<%= render 'form' %>

<%= link_to 'Back', user_orders_path(@user) %>

2 Answers 2

2

When you write this:

<%= form_for(@order) do |f| %>

Rails is looking for orders_path, but you don't really have such a route as you have defined your orders resource nested under the users resource. So, you have to pass both: @user and @order to the form like this:

<%= form_for([@user, @order]) do |f| %>
    # your code goes here
<% end %>

If you write the form this way, then Rails will look for this path: user_orders_path which will match your defined route and will work and you won't get this error anymore:

undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>

So, this will fix your current issue. But, there is another issue in your new.html.erb file where you currently have this:

<%= link_to 'Back', user_orders_path %>

You have to pass the @user as an argument to this method:

<%= link_to 'Back', user_orders_path(@user) %>

otherwise, it will fail again.

Hope this helps to solve your problems.

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

2 Comments

Really? After you made the changes that mentioned in my answer you are getting the SAME error? At least it should be a DIFFERENT error if did not fix your problem.
could you please show your controller and view files?
0

Since orders is nested on top of users, the form_for declaration will need to include both the user, and the order objects.

<%= form_for([@user, @order]) do |f| %>

Your new template will also need to have a reference to the given user.

<%= link_to 'Back', user_orders_path(@user) %>

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.