2016-07-20 10 views
0

ちょっとすべて私のコードで、私はすべてのトピックのインデックスにリダイレクトしています。理論的には、ページにリダイレクトしたいと思います。
これが今の私はちょうどでスタンドとしてtopics_pathを使用しています、このページのための私のコントローラである。トピック/ページを表示するにはどうすればいいですか?

class LikesController < ApplicationController 
    def index 
    end 
    def create 
    @bookmark = Bookmark.find(params[:bookmark_id]) 
    like = current_user.likes.build(bookmark: @bookmark) 
    if like.save 
     flash[:notice] = "Successfully liked bookmark." 
    else 
    flash.now[:alert] = 'Error in liking bookmark. Please try again.' 
    end 
    redirect_to topics_path 
end 

def destroy 
    @bookmark = Bookmark.find(params[:bookmark_id]) 
    like = current_user.likes.find(params[:id]) 
    # Get the bookmark from the params 
    # Find the current user's like with the ID in the params 

    if like.destroy 
    flash[:notice] = "Successfully unliked bookmark." 
    else 
    flash.now[:alert] = 'Error in unliking bookmark. Please try again.' 
    end 
    redirect_to topics_path 
end 
end 

これはあなたの場合、私はredirect_to
bookmarks_show GET /bookmarks/show(.:format) bookmarks#show

+1

あなたは 'TopicsController' – SpunkyLive

+0

' defを、私は、なぜ私はよく分からない
'@topics = Topic.all'
'認可(@topics) '
' end'
をindex'であなたの 'index'方法を共有することができますマークダウン構文が正しく機能していません。 – SinGar

答えて

1

にしたレーキルートからのラインです特定のトピックのページにリダイレクトしたい場合は、リダイレクトで使用できるように、topic_idをパラメータとして渡す必要があります。

あなたが例えばを使用しているフォーム/リンクにそれを追加します。 (注:完全に明らかにあなたのコードが異なるだろう、これを構成する)

<% form_for @like do |f| %> 
    <%= f.hidden_field :topic_id, @topic.id %> 

その後、あなたの中でアクションを作成し、あなただけ使用してリダイレクトその例:

def create 
    @bookmark = Bookmark.find(params[:bookmark_id]) 
    like = current_user.likes.build(bookmark: @bookmark) 
    if like.save 
    flash[:notice] = "Successfully liked bookmark." 
    else 
    flash.now[:alert] = 'Error in liking bookmark. Please try again.' 
    end 
    redirect_to topic_path(:id => params[:topic_id]) 
end 

注:あなたは、いくつかの他のページ(例えばブックマークページ)を使用したい場合は、代わりに...これは「一般的なHOWTO」ではない、あなたが見るように、「まさにこのコードを使用していることを使用しますここに」:)

関連する問題