从 erb 切换到 haml 后,视图模板在重定向时重复

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

我正在开发一个小型抽认卡应用程序,作为练习以更好地使用 Rails。 在我从

.erb
切换到
haml
作为模板语言后,当应用程序使用
redirect_to
重定向时会发生奇怪的事情。

  1. 浏览器历史记录不变,地址栏中的 URL 保持不变
  2. 无需使用布局和新视图重新渲染整个页面,只需将新视图添加到当前布局即可。所以我看到布局+旧视图+新视图。 (但我不想看到老样子)

这里是起始视图的代码:

# app/views/dashboards/show.html.erb
<h1>Dashboard</h1>

<h2>Cards ready for Training</h2>
<ul>
  <% @topics.each do |topic| %>
    <li><%= topic %>: <%= topic.cards_ready_for_training.count %> Cards ready for training <%= button_to "Start Training", trainings_path(training: { topic_id: topic.id }) %></li>
  <% end %>
</ul>

...

用户点击“开始训练”后,

TrainingsController#create
被执行。

# app/controllers/trainings_controller.rb  
def create
    @training = TrainingFactory.create_from_cards(cards_scope)

    if @training.persisted?
      training_progress = TrainingProgress.new(@training)
      redirect_to edit_training_card_url(training_progress.next_card), notice: "Training was successfully created."
    else
      render :new, status: :unprocessable_entity
    end
  end

我们将请求重定向到

TrainingCardsController#edit

# app/controllers/training_cards_controller.rb
 def edit
    load_training_progress
  end

呈现

edit.haml
文件:

# app/views/training_cards/edit.haml
%h3= "#{@training_progress.pending_cards_count} cards left"

%h2= @training_card.question

= render "form", training_card: @training_card

%br

%div
  = link_to "Back to Dashboard", root_path

但不是重新渲染整个页面,而是将 edit.haml 视图插入到当前页面中。

如果我将 edit.haml 转换回 erb,一切都会按预期进行。

我还尝试将所有视图模板转换为 haml,包括 layout.html.erb。但这也没有帮助。

另外:我只在使用

redirect_to
重定向时遇到这个问题,而 get 请求中视图的默认呈现工作正常。

ruby-on-rails ruby haml erb
© www.soinside.com 2019 - 2024. All rights reserved.