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.