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

huangapple go评论64阅读模式
英文:

After switching from erb to haml the view templates are duplicated on redirects

问题

以下是您要翻译的内容:

  1. 我正在开发一个小型的单词卡应用程序,以提高我的Rails技能。

  2. 在我从.erb转换为haml作为模板语言后,当应用程序使用redirect_to进行重定向时出现了奇怪的问题。

    1. 浏览器历史记录不会更改,地址栏中的URL保持不变。
    2. 而不是重新呈现整个页面,包括布局和新视图,新视图只是插入到当前页面的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.

  1. The Browser History doesnt change, the URL in the address bar stays the same
  2. 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
&lt;h1&gt;Dashboard&lt;/h1&gt;

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

...

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: &quot;Training was successfully created.&quot;
    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= &quot;#{@training_progress.pending_cards_count} cards left&quot;

%h2= @training_card.question

= render &quot;form&quot;, training_card: @training_card

%br

%div
  = link_to &quot;Back to Dashboard&quot;, 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.

huangapple
  • 本文由 发表于 2023年3月21日 01:58:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定