16331

Rails Redirect to an Anchor Tag from the Controller

I am using Comments table as polymorphic associations. When i save the comment and it redirects to the respective associated model. I want it to pass an anchor tag to auto scroll down to display the comments. Am trying the following but the anchor tag is not passing to the url

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

<strong>Rake Routes</strong>

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

Answer1:

Remove the # symbol in your anchor string and call anchor: within the url helper method: redirect_to comments_path(anchor: 'anchor_tag')

Answer2:

Alright, from rails DOC url_for() method should return URL of given object.

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

if its nested then url_for([@product, @comment])

Recommend

  • Using Unicode character (plus minus sign) inside ggplot2
  • ActionController::RoutingError (No route matches [GET] “/images/medium/missing.png”):
  • Rails 4 Strong Parameters opposite permit all attributes
  • linq joint type inference failed to call 'join' error
  • Michael Hartl Rails Tutorial - undefined method 'sign_in'
  • R: Reshape from long to wide, order of reshaped columns as original column order [duplicate]
  • Devise methods undefined in Rails project
  • Stringify Keys Error with Devise and Declarative Testing with RSpec in Rails 3.1
  • Devise: undefined method `user_signed_in?' for ApplicationController:Class
  • How to wait for all async tasks to finish in Node.js?
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • Segmentation Fault on MySQL2 / Ruby 1.9.3 / Rails 3.2
  • Sesame : how to remove the inference during queries?
  • HttpURLConnection.getOutputStream() takes 20 seconds. Why?
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Azure table store snapshot/backup capability
  • Why use database factory in asp.net mvc?
  • Android Google Maps API v2 start navigation
  • System.InvalidCastException: Specified cast is not valid
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • WinForms: two way TextBox problem
  • Cassandra Data Model
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • R: gsub and capture
  • bootstrap to use multiple ng-app
  • SetUp method failed while running tests from teamcity
  • Comma separated Values
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • how does django model after text[] in postgresql [duplicate]
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • How to load view controller without button in storyboard?