のレコードを取得します。私はすべての認証設定を持っており、ユーザがログインすると、アプリケーションコントローラのオブジェクトcurrent_userを設定します。これで、ユーザーが自分のサイト(サイトモデルには「belongs_to:user ")。私は3つのコントローラを持っている現在のユーザー
class SitesController < ApplicationController
def index
#this was Site.all, but I changed it. Is there a better way to do this?
@sites = Site.find_all_by_user_id(current_user.id)
# respond to ... etc
end
# Now, for show, edit and update, I want to ensure the site belongs to the user. How can I add that?
def show
@site = Site.find(params[:id])
# respond to ... etc
end
さらに詳しい情報が必要な場合(モデル、コントローラ全体など)、私に知らせて、私はそれを広告します!
ありがとうございます!
に関連付けられているすべてのサイトを取得するにはいいね!私はSite.whereをやったでしょうが、あなたのアプローチに間違ったことはありません。表示/編集などのために、あなたはidを探していますが、ユーザーIDにもwhere節を追加します。 – aishwarya
私の主な質問は、ユーザーが自分のサイトを編集できるようにすることです。私が追加しようとしました: はDEF @site = current_user.sites.find([:ID]のparams)編集 エンド をしかし、私は得る:mysqlの::エラー: '順序句' 内の不明な列 'published_at':SELECT 'si etc – execv
Site.whereに変更(:id => params [:id]、:user_id => current_user)?私は鉱山を掲載するまで – aishwarya