Devise nested resource view shows no method error

2019-04-17 08:07发布

My nested resource is farm. In my routes I have:

resources :users do
  resource :farm
end


devise_for :users, :path => 'accounts'

So, the devise paths for sign up, etc are working and not making problems. But when I try to make a new farm, I get this error:

undefined method `farm_user_path' for #<#<Class:0x463d8f8>:0x46493e8>

I am accessing it via:

<%= link_to "New Farm", new_user_farm_path(current_user) %>

In my farm controller, I have:

class FarmsController < ApplicationController
# GET /farms
# GET /farms.json

include Devise::Controllers::Helpers
helper_method :current_user

def new
@farm = Farm.new
@user = User.find(current_user)

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @farm }
end
end

...
end

And my form for making new farm is :

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

All of the associations and routes are ok. What am I missing?

1条回答
爷、活的狠高调
2楼-- · 2019-04-17 08:55

Found the solution. Problem was that my form needed to look like this:

<%= form_for([@user, @farm], :url=> new_user_farm_path(@user)) do |f| %>
查看更多
登录 后发表回答