2016-04-05 12 views
2

私は、多態性の関連付けとしてコメントテーブルを使用しています。私はコメントを保存し、それぞれの関連するモデルにリダイレクトします。コメントを表示するためにアンカータグを自動的にスクロールダウンする必要があります。しようとしています以下のが、アンカータグは、あなたのアンカー文字列に#記号を削除し、URLヘルパーメソッド内anchor:を呼び出すURLに渡すRailsコントローラからアンカータグにリダイレクト

@comment = @commentable.comments.new comment_params 
    @comment.user = current_user 
    @comment.save 
    redirect_to @commentable, :anchor => '#comments' 

レーキルート

Prefix Verb URI Pattern         Controller#Action 
        rate POST /rate(.:format)        rater#create 
     new_user_session GET /users/sign_in(.:format)      devise/sessions#new 
      user_session POST /users/sign_in(.:format)      devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy 
      user_password POST /users/password(.:format)      devise/passwords#create 
     new_user_password GET /users/password/new(.:format)     devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format)    devise/passwords#edit 
         PATCH /users/password(.:format)      devise/passwords#update 
         PUT /users/password(.:format)      devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)      devise/registrations#cancel 
     user_registration POST /users(.:format)        devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)      devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)       devise/registrations#edit 
         PATCH /users(.:format)        devise/registrations#update 
         PUT /users(.:format)        devise/registrations#update 
         DELETE /users(.:format)        devise/registrations#destroy 
        root GET /           welcome#index 
       artists GET /artists(.:format)       artists#index 
         POST /artists(.:format)       artists#create 
       new_artist GET /artists/new(.:format)      artists#new 
      edit_artist GET /artists/:id/edit(.:format)     artists#edit 
        artist GET /artists/:id(.:format)      artists#show 
         PATCH /artists/:id(.:format)      artists#update 
         PUT /artists/:id(.:format)      artists#update 
         DELETE /artists/:id(.:format)      artists#destroy 
      album_comments GET /albums/:album_id/comments(.:format)   albums/comments#index 
         POST /albums/:album_id/comments(.:format)   albums/comments#create 
     new_album_comment GET /albums/:album_id/comments/new(.:format)  albums/comments#new 
     edit_album_comment GET /albums/:album_id/comments/:id/edit(.:format) albums/comments#edit 
      album_comment GET /albums/:album_id/comments/:id(.:format)  albums/comments#show 
         PATCH /albums/:album_id/comments/:id(.:format)  albums/comments#update 
         PUT /albums/:album_id/comments/:id(.:format)  albums/comments#update 
         DELETE /albums/:album_id/comments/:id(.:format)  albums/comments#destroy 
        albums GET /albums(.:format)        albums#index 
         POST /albums(.:format)        albums#create 
       new_album GET /albums/new(.:format)       albums#new 
       edit_album GET /albums/:id/edit(.:format)     albums#edit 
        album GET /albums/:id(.:format)       albums#show 
         PATCH /albums/:id(.:format)       albums#update 
         PUT /albums/:id(.:format)       albums#update 
         DELETE /albums/:id(.:format)       albums#destroy 
      song_comments GET /songs/:song_id/comments(.:format)   songs/comments#index 
         POST /songs/:song_id/comments(.:format)   songs/comments#create 
     new_song_comment GET /songs/:song_id/comments/new(.:format)  songs/comments#new 
     edit_song_comment GET /songs/:song_id/comments/:id/edit(.:format) songs/comments#edit 
      song_comment GET /songs/:song_id/comments/:id(.:format)  songs/comments#show 
         PATCH /songs/:song_id/comments/:id(.:format)  songs/comments#update 
         PUT /songs/:song_id/comments/:id(.:format)  songs/comments#update 
         DELETE /songs/:song_id/comments/:id(.:format)  songs/comments#destroy 
        songs GET /songs(.:format)        songs#index 
         POST /songs(.:format)        songs#create 
       new_song GET /songs/new(.:format)       songs#new 
       edit_song GET /songs/:id/edit(.:format)      songs#edit 
        song GET /songs/:id(.:format)       songs#show 
         PATCH /songs/:id(.:format)       songs#update 
         PUT /songs/:id(.:format)       songs#update 
         DELETE /songs/:id(.:format)       songs#destroy 
         GET /get_albums_of_artist/:artist_id(.:format) songs#get_albums_of_artist 
+0

uは、@commentable redirect_to 'ハッシュタグなしで使用しようとしました:アンカーは=>「comments'' – 7urkm3n

+0

はいdoesntの仕事:(' @のcommentable'は、Aリダイレクト –

+0

ページ – 7urkm3n

答えて

3

されていません。redirect_to comments_path(anchor: 'anchor_tag')

+0

その多形性が私には曖昧なパスを生成できないので、ルートで質問を更新しました –

+0

そのURLをプレーンな文字列としてredirectし、アンカーを文字列URL '/ my_url#'に追加する場合は? –

+0

私は何をしようとしています。再構成は多相ルートから派生する@commentableに起こっています –

2

大丈夫、レールよりDOCurl_for()メソッドは、指定されたオブジェクトのURLを返す必要があります。

redirect_to "#{url_for(@commentable)}#comments" 

そのネストされた場合は、url_for([@product, @comment])

関連する問題