Rails 快讯
Rails flash message
我正在 Rails 上构建一个应用程序,用户可以在该应用程序中创建一个测试,转到该测试的显示视图并填写一个包含问题答案的表单。如果问题的答案与 "correct_answer"(在控制器中定义)相匹配,将会出现提示答案正确的闪烁消息,并且会出现一个继续 root_path 的按钮。如果答案错误,闪光灯会显示 "Wrong answer."
我的问题是即使没有给出答案,闪现信息也会说错答案。我只希望在用户提交表单后显示该消息。我明白为什么会这样,我只是不确定如何解决它。这是测试的显示视图:
<div class="col-md-8 col-md-push-2">
<h4>Current Score: <%= @test.score %></h4>
<br /><br />
<div class="form_group">
<%= form_tag test_path(@test), :method=> 'get' do %>
<h4>What is my first name?</h4>
<div class="form-group">
<%= text_field_tag :answer, params[:answer], class: 'form-control' %>
</div>
<% if !flash[:success] %>
<div class="form-group">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
</div>
<% if flash[:success] %>
<%= link_to "Continue", root_path, class: "btn btn-success pull-right" %>
<% end %>
</div>
这是用于测试的控制器,其中包含有问题的显示操作:
class TestsController < ApplicationController
def index
@test = Test.new
@tests = Test.all
end
def show
@test = Test.find(params[:id])
correct_answer = "jack"
user_answer = params[:answer]
if user_answer == correct_answer
flash.now[:success] = "That is correct!"
new_score = @test.score += 1
@test.update(score: new_score)
elsif params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
end
def create
@test = Test.create(test_params)
if @test.save
redirect_to test_path(@test)
flash[:success] = "Test created"
else
flash[:danger] = "There was a problem"
render "index"
end
end
def destroy
@test = Test.find(params[:id])
if @test.destroy
flash[:success] = "Your test was removed"
redirect_to root_path
end
end
private
def test_params
params.require(:test).permit(:score, :user_id)
end
end
有更好的方法吗?如果没有,我能否以某种方式阻止闪现消息出现在初始加载中?我只希望它在提交表单后出现。提前致谢。
所以你的闪光灯被触发了,因为 params[:answer]
是未定义的,而不是 ==
到 jack
。
你说:
My problem is that the flash message will say wrong answer even if no answer has been given.
但是对于这个结果你的逻辑是错误的,你的代码说 params[:answer]
已经定义了,它不可能是因为表单还没有呈现。
也许你的情况应该是:
elsif params[:answer].present? && params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
传统上,显示表单和 POST
表单是单独的操作,这就是为什么您会在任何事情发生之前看到这个闪光。
我正在 Rails 上构建一个应用程序,用户可以在该应用程序中创建一个测试,转到该测试的显示视图并填写一个包含问题答案的表单。如果问题的答案与 "correct_answer"(在控制器中定义)相匹配,将会出现提示答案正确的闪烁消息,并且会出现一个继续 root_path 的按钮。如果答案错误,闪光灯会显示 "Wrong answer."
我的问题是即使没有给出答案,闪现信息也会说错答案。我只希望在用户提交表单后显示该消息。我明白为什么会这样,我只是不确定如何解决它。这是测试的显示视图:
<div class="col-md-8 col-md-push-2">
<h4>Current Score: <%= @test.score %></h4>
<br /><br />
<div class="form_group">
<%= form_tag test_path(@test), :method=> 'get' do %>
<h4>What is my first name?</h4>
<div class="form-group">
<%= text_field_tag :answer, params[:answer], class: 'form-control' %>
</div>
<% if !flash[:success] %>
<div class="form-group">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
</div>
<% if flash[:success] %>
<%= link_to "Continue", root_path, class: "btn btn-success pull-right" %>
<% end %>
</div>
这是用于测试的控制器,其中包含有问题的显示操作:
class TestsController < ApplicationController
def index
@test = Test.new
@tests = Test.all
end
def show
@test = Test.find(params[:id])
correct_answer = "jack"
user_answer = params[:answer]
if user_answer == correct_answer
flash.now[:success] = "That is correct!"
new_score = @test.score += 1
@test.update(score: new_score)
elsif params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
end
def create
@test = Test.create(test_params)
if @test.save
redirect_to test_path(@test)
flash[:success] = "Test created"
else
flash[:danger] = "There was a problem"
render "index"
end
end
def destroy
@test = Test.find(params[:id])
if @test.destroy
flash[:success] = "Your test was removed"
redirect_to root_path
end
end
private
def test_params
params.require(:test).permit(:score, :user_id)
end
end
有更好的方法吗?如果没有,我能否以某种方式阻止闪现消息出现在初始加载中?我只希望它在提交表单后出现。提前致谢。
所以你的闪光灯被触发了,因为 params[:answer]
是未定义的,而不是 ==
到 jack
。
你说:
My problem is that the flash message will say wrong answer even if no answer has been given.
但是对于这个结果你的逻辑是错误的,你的代码说 params[:answer]
已经定义了,它不可能是因为表单还没有呈现。
也许你的情况应该是:
elsif params[:answer].present? && params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
传统上,显示表单和 POST
表单是单独的操作,这就是为什么您会在任何事情发生之前看到这个闪光。