英文:
After switching from erb to haml the view templates are duplicated on redirects
问题
以下是您要翻译的内容:
-
我正在开发一个小型的单词卡应用程序,以提高我的Rails技能。
-
在我从
.erb
转换为haml
作为模板语言后,当应用程序使用redirect_to
进行重定向时出现了奇怪的问题。- 浏览器历史记录不会更改,地址栏中的URL保持不变。
- 而不是重新呈现整个页面,包括布局和新视图,新视图只是插入到当前页面的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请求中默认呈现视图正常运作。
英文:
I am working on a small flashcards application as an exercise to get better with rails.
After I switched from .erb
to haml
as templating language, strange things happen when the application redirects with redirect_to
.
- The Browser History doesnt change, the URL in the address bar stays the same
- Instead of re-rendering the whole page with the layout and the new view, the new view is simply inserted at the end of the DOM of the current page. So I see the layout + the old view + the new view. (But I dont want to see the old view)
Here is the code for the starting view:
# 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>
...
After the user clicks on "Start Training" the TrainingsController#create
is executet.
# 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
the we redirect the request to TrainingCardsController#edit
# app/controllers/training_cards_controller.rb
def edit
load_training_progress
end
which renders the edit.haml
file:
# 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
But instead of rerendering the whole page, the edit.haml view is just inserted into the current page.
If I convert the edit.haml back to erb, everything works as expected.
I have also tried converting all view templates to haml, including layout.html.erb. But that didn't help either.
Also: I only get that problem on redirects with redirect_to
, while the default rendering of views in get requests works fine.
答案1
得分: 0
好的,我找到了解决这个问题的办法,但我不明白为什么这个办法可以解决整个问题。也许有更多经验的人可以想出原因并描述问题的根本原因。
从应用程序中移除 Turbolinks 后,重定向功能恢复正常。
英文:
Okay I found a solution to the problem, however I do not understand why this fixes the whole thing. Maybe someone with more experience has an idea and can describe the cause of the problem.
After removing Turbolinks from the application, redirection works fine again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论