【问题标题】:Rails Routing new association pathRails Routing 新的关联路径
【发布时间】:2018-12-15 11:49:26
【问题描述】:

我有家庭,我有代币——家庭拥有代币

我有一个家庭索引,并为我使用的家庭创建一个新令牌:

 = link_to 'Issue', new_household_token_path, class: 'btn'

Rake 路线显示:

 new_household_token GET  /households/:household_id/tokens/new(.:format)     tokens#new

但我明白了:

 No route matches {:action=>"new", :controller=>"tokens"}

但看起来我确实有那条路线

令牌控制器有:

def new
    @token = Token.new

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

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails routes ruby-on-rails-3.2


    【解决方案1】:

    您缺少:household_id。通过查看路线,您可以看到household_id 必须包含在/households/:household_id/tokens/new(.:format) 中,才能访问您的TokensController#new。将您的household_id 传递给您的路线,例如(只要@household 不是nil

    = link_to 'Issue', new_household_token_path(@household.id), class: 'btn'
    

    【讨论】:

    • 谢谢——正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多