在Rails应用程序中创建操作后如何使用带有ID的重定向URL进行重定向?

问题描述 投票:0回答:1

我正在使用Foundation-zurb选项卡功能,并从不同模型渲染数据。创建一些财务信息后,它将重定向到带有在基础类中定义的活动选项卡的显示页面。创建一些财务信息后,我如何重定向到财务标签?

project#show.html.erb

  <div class="row">
    <div class="columns">
      <ul class="tabs" data-tabs id="example-tabs">
        <li class="tabs-title is-active"><a href="#panel1" aria-selected="true">Project Activities</a></li>
        <li class="tabs-title"><a href="#panel2">Project Financial</a></li>
      </ul>

      <div class="tabs-content" data-tabs-content="example-tabs">
        <div class="tabs-panel is-active" id="panel1">
          <%= render "projects/project_activities" %>
        </div>
        <div class="tabs-panel" id="panel2">
          <%= render "financials/index" %>
        </div>

financials_controller.rb

  def create
    @financial = @project.financials.build(financial_params)

    respond_to do |format|
      if @financial.save
        format.html { redirect_to project_path(@project), notice: 'Financial was successfully created.' }
        format.json { render :show, status: :created, location: @financial }
      else
      end
    end
  end
ruby-on-rails ruby-on-rails-5 zurb-foundation
1个回答
1
投票

只需使用anchor选项作为URL辅助程序

redirect_to project_path(@project, anchor: 'panel2')

您需要在选项卡上添加data-deep-link选项以将当前状态存储在URL中,并允许用户在页面加载时使用带有哈希值的URL打开特定的选项卡

<ul class="tabs" data-tabs data-deep-link="true" id="example-tabs">

您可以在docs中找到更多有用的选项

© www.soinside.com 2019 - 2024. All rights reserved.