link_to with anchor渲染帖子,然后是锚点

时间:2016-05-15 07:57:50

标签: ruby-on-rails ruby

当我使用时:

<%= link_to reply.pid, board_post_path(@board.slug, post, anchor: reply.pid) %>

浏览器首先转到/board.slug/post.pid,然后转到/board.slug/post.pid#reply.pid,需要按两次按钮才能返回上一页。

我该如何解决这个问题,为什么会这样?

posts_controller.rb:

class PostsController < ApplicationController

  def show
    @board = Board.friendly.find(params[:board_id])
    @boards = Board.all
    @post = Post.includes(:board).where('boards.slug' => params[:board_id]).friendly.find(params[:id])
  end

  def create
    @board = Board.friendly.find(params[:board_id])
    @postf = Post.all
    @post = @board.posts.create(post_params)
    if @post.email.to_s == 'stay'
      @post.update_attributes(:email => nil)
      redirect_to action: "show", id: @post.id
    elsif !@post.save
      flash[:error] = @post.errors.full_messages
      redirect_to :back
    else
      redirect_to :back
    end
  end

  def destroy
    @post = Post.friendly.find(params[:id])
    @post.destroy
    redirect_to :back
  end

  private

  def post_params
    params.require(:post).permit(:name, :email, :subject, :comment, :post_file, :post_file_original_name)
  end

end

0 个答案:

没有答案