仅访问索引视图中的当前记录

时间:2013-05-26 02:46:43

标签: ruby-on-rails

我有一个名为“Profile”的模型。

我想仅根据current_user.id显示个人资料,因此当用户转到/个人资料时,它会“显示”个人资料,而不是列出个人资料。

配置文件在模型中有user_id列。

Profile的控制器当前设置为:

class ProfileController < ApplicationController
  before_filter :authenticate_user!

  def index
    @profiles = Profile.where(user_id:current_user.id)
  end

  def show
    @profile = Profile.find(params[:id])
  end

我查看了Active Records查询方法,但我找不到合适的答案。

1 个答案:

答案 0 :(得分:1)

看看here,了解奇异资源。顺便提一下,它谈到了您的具体用例。假设您有current_user和has_one关系设置,那么您可以调用@profile = current_user.profile

更详细一点,如果将resources:profile添加到路由文件中,则只需链接到profile_path即可。这将路由到show动作。只需确保设置current_user并检索利用has_one关系的配置文件。

希望这有帮助。

相关问题