如何在 rails 应用程序中创建操作后使用带有 id 的重定向 url 进行重定向?

how to redirected using redirection url with id after create action in rails application?

我正在使用 foundation-zurb 选项卡功能,并渲染来自不同模型的数据。当创建一些财务时,它会重定向到显示页面,其中包含在基础 class 中定义的活动选项卡。创建某些财务时如何重定向到财务选项卡?

项目#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

只需为 url 助手使用 anchor 选项

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

中找到更多有用的选项