Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect_to with :anchor. The :anchor gets lost on the Show action but works fine on Create

In my Rails Forum app, when creating a new Post in a Topic, it will redirect to the topic_path with additional arguments for the page index and the anchor in order to scroll to the post. Like so:

app/controllers/posts_controller.rb

def create
  @topic = Topic.find(params[:topic_id])
  @post = @topic.posts.build(post_params.merge({user_id: current_user.id}))

  if @post.save
    flash[:success] = "Post Created"
    redirect_to topic_path(@topic, :page => @post.page, :anchor => @post.anchor) 
  else
    render 'new'
  end
end

The url after the redirect is: http://localhost:3000/topics/1?page=3#post-1364

But I do the same thing in the Show action for the Posts controller. Since i don't want to show Posts on their own, the action simply redirects to the Topic with the page index and post anchor.

app/controllers/posts_controller.rb

# Post are not displayed on their own. Showing one will jump to the post inside its topic
def show
  post = Post.find(params[:id])
  redirect_to topic_path(post.topic, :page => post.page, :anchor => post.anchor)
end

But the url after calling the show method for a post does not include the anchor. It does include the page though: http://localhost:3000/topics/1?page=3 I debugged into the show method and the post.anchor is being resolved correctly.

My terminal output shows that the anchor gets lost for some reason

Started GET "/posts/1364" for 127.0.0.1 at 2014-08-13 10:03:31 -0700
Processing by PostsController#show as HTML
  Parameters: {"id"=>"1364"}
  Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "1364"]]
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1  [["id", 1]]
  Post Load (1.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."topic_id" = ?  [["topic_id", 1]]
Redirected to http://localhost:3000/topics/1?page=3#post-1364
Completed 302 Found in 165ms (ActiveRecord: 3.7ms)


Started GET "/topics/1?page=3" for 127.0.0.1 at 2014-08-13 10:03:32 -0700

Here are my routes for anything involving Posts if it helps.

    topic_posts GET    /topics/:topic_id/posts(.:format)      posts#index
                POST   /topics/:topic_id/posts(.:format)      posts#create
 new_topic_post GET    /topics/:topic_id/posts/new(.:format)  posts#new
      edit_post GET    /posts/:id/edit(.:format)              posts#edit
           post GET    /posts/:id(.:format)                   posts#show
                PATCH  /posts/:id(.:format)                   posts#update
                PUT    /posts/:id(.:format)                   posts#update
                DELETE /posts/:id(.:format)                   posts#destroy
like image 567
Adam Avatar asked Aug 13 '14 17:08

Adam


1 Answers

Check this questions:

URL Fragment and 302 redirects

Is a 302 Redirect to relative URL valid, or invalid?

  redirect_to topic_path(post.topic, :page => post.page, :anchor => post.anchor, :status => 303) 

should work.

like image 104
Andrey Artemyev Avatar answered Nov 18 '22 06:11

Andrey Artemyev