我正在开发一个小的抽认卡应用程序,作为更好地使用Rails的练习。在我从.erb
切换到haml
作为模板语言后,当应用程序使用redirect_to
重定向时,奇怪的事情发生了。
1.浏览器历史记录不变,地址栏中的URL保持不变
1.而不是重新渲染整个页面的布局和新的视图,新的视图只是简单地插入到当前页面的DOM的末尾。所以我看到布局+旧视图+新视图。(但我不想看到旧视图)
下面是起始视图的代码:
# 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请求中默认呈现的视图工作正常。
1条答案
按热度按时间eivnm1vs1#
好吧,我找到了解决问题的方法,但是我不明白为什么这可以解决整个问题。也许有更多经验的人有一个想法,可以描述问题的原因。
在从应用程序调用removing Turbolinks之后,重定向再次正常工作。