2016-04-22 2 views
0

私はアプリケーションを作成してRoRを学習しようとしていますが、私は問題に遭遇しました。私の方法に欠陥があるか、それが正しい方法であればわかりませんが、違う。私はそれがインスタンス変数である変数と関係があると思います。それは他のコントローラでは呼び出されませんが、そこに取得する方法がわかりません。他のコントローラのtodosにアクセスする方法は?

とにかく問題がある - そう

私はドスコントローラを持っている、モデル、ビューなどのRailsでの足場を経由して設定するが、私は彼らの「ダッシュボード」で、各ユーザにドスを表示できるようにしたいです彼らがログインしたときに話すことができます。したがって、私はダッシュボードコントローラにもtodosが必要であると想定しています。ここで

は私のダッシュボードコントローラは、ここで私は私のドスを呼んでいるが、私はビューでそれらを呼び出すときに表示されていない

class DashboardController < ApplicationController 
    def home 
    @todos = current_user.todos 
    end 
end 

です。

class TodosController < ApplicationController 
    before_action :set_todo, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    # GET /todos 
    # GET /todos.json 
    def index 
    @todos = current_user.todos 
    end 

    # GET /todos/1 
    # GET /todos/1.json 
    def show 
    end 

    # GET /todos/new 
    def new 
    @todo = Todo.new 
    end 

    # GET /todos/1/edit 
    def edit 
    end 

    # POST /todos 
    # POST /todos.json 
    def create 
    @todo = current_user.todos.new(todo_params) 

    respond_to do |format| 
     if @todo.save 
     format.html { redirect_to @todo, notice: 'Todo was successfully created.' } 
     format.json { render :show, status: :created, location: @todo } 
     else 
     format.html { render :new } 
     format.json { render json: @todo.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /todos/1 
    # PATCH/PUT /todos/1.json 
    def update 
    respond_to do |format| 
     if @todo.update(todo_params) 
     format.html { redirect_to @todo, notice: 'Todo was successfully updated.' } 
     format.json { render :show, status: :ok, location: @todo } 
     else 
     format.html { render :edit } 
     format.json { render json: @todo.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /todos/1 
    # DELETE /todos/1.json 
    def destroy 
    @todo.destroy 
    respond_to do |format| 
     format.html { redirect_to todos_url, notice: 'Todo was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_todo 
     @todo = Todo.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def todo_params 
     params.require(:todo).permit(:title, :item) 
    end 
end 

がどのように私はダッシュボードに私のToDo項目を表示するに行くか

と私のドス足場?任意のヘルプ

+0

'current_user'は現在ログインしているユーザーを返すはずです。' current_user'は 'DashboardController'で利用できますか? – dp7

+0

私はそう信じますが、私は100%確実ではありません - どうすれば確認できますか?申し訳ありませんが、それは私の最初のプロジェクトです –

+0

'before_action:authenticate_user!' - 'DashboardController'にあるように' DashboardController'に追加してください – dp7

答えて

1

あなたはちょうどあなたがTodosControllerでそれを持っている方法のようDashboardController

before_action :authenticate_user! 

を追加する必要があります。

0

ため

おかげで、ダッシュボードのコントローラの現在のユーザーを持っていますか?それを処理する方法を決定する必要があります。サインインが必要な場合や、if else文を使用してください。

def home 
    if current_user 
    @todos = current_user.todos 
    end 
end 
+0

私はbefore_action:authenticate_userをやりました!これをtodos.rbファイルで処理するには、ユーザーはダッシュボードにアクセスするためにログインする必要があります。それ以外の場合は、ホームページにリダイレクトされます。これは正しい方法ですか? –

関連する問題