polymorphic_path无法生成正确的路径 [英] polymorphic_path not generating correct path

查看:83
本文介绍了polymorphic_path无法生成正确的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用名为polymorphic_path的rails方法,但网址错误. 我与学生和房东的多态性关联,他们都是通过可使用的用户.

I am trying to user a rails method called polymorphic_path but I am getting the wrong url. My polymorphic association is with Students and Landlords who are both Users through userable.

这是我的模特:

class User < ActiveRecord::Base
  belongs_to :userable, polymorphic: true
end

class Student < ActiveRecord::Base
  has_one :user, :as => :userable
end

class Landlord < ActiveRecord::Base
  has_one :user, :as => :userable
end

我有一个名为current_user的变量,用于保存User对象.以下行:

I have a variable called current_user holding the User object. The following line:

<%= link_to "Profile", polymorphic_path(current_user) %>

为我提供了网址"users/22",而不是返回学生/房东的网址.

gives me the url "users/22" instead of returning the Student/Landlord url.

这是我的route.rb文件,如果有帮助的话.

Here is my routes.rb file if that helps..

resources :users
resources :students
resources :landlords

我要去哪里错了? 谢谢!

Where am I going wrong? Thanks!

推荐答案

好,知道了!而且解决方案非常痛苦……

Ok, I got it! And the solution was painfully obvious...

<%= link_to "Profile", polymorphic_path(current_user.userable) %>
<%= link_to "Edit", edit_polymorphic_path(current_user.userable) %>

这篇关于polymorphic_path无法生成正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆