0

我有一个名为“配置文件”的模型。

我只想显示基于 current_user.id 的配置文件,因此当用户转到 /profiles 时,它将“显示”配置文件而不是列出配置文件。

Profile 在模型中有 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 查询方法,但找不到合适的答案。

4

1 回答 1

1

Take a look here to read about singular resources. Incidentally, it talks about your specific use case. Assuming you have the current_user and a has_one relationship setup, you could then just call @profile = current_user.profile

To be more verbose, if you add resource :profile to your routes file, you can just link to profile_path. This will route to the show action. Simply ensure current_user is set and retrieve the profile leveraging the has_one relationship.

Hope this helps.

于 2013-05-26T02:54:42.867 回答