【发布时间】:2020-02-22 21:21:17
【问题描述】:
您好,我想在创建八卦时创建成功警报并返回我的主页,或者在验证失败时发出危险警报
我刚刚成功设置了错误警报。
这是我的八卦控制器:
class GossipsController < ApplicationController
def index
@gossips = Gossip.all
end
def show
@gossip = Gossip.find(params[:id])
end
def new
@error = false
end
def create
@gossip = Gossip.new(title: params[:title], content: params[:content], user: User.find(182))
if @gossip.save
redirect_to root_path
else
@error = true
render "new"
end
end
end
这是我对新的看法:
<% if @error %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error</strong>
<ul>
<% @gossip.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
<h2>Create your own gossip !</h2> <br><br>
<%= form_tag url_for(action: 'create'), method: "post" do %>
<%= label_tag 'Title :' %> <br>
<%= text_field_tag 'title'%> <br><br>
<%= label_tag 'Content :' %> <br>
<%= text_area_tag 'content'%> <br><br>
<%= submit_tag "Create Gossip" %>
<% end %>
我尝试为成功警报做同样的事情,但如果我在控制器中放置 @success = true 并在索引视图中放置 <% if @success %> 则不起作用。我没有任何想法。
如果您需要我的代码的某些部分,请告诉我。
我已经尝试过使用 flash 但这不起作用,并且我想要与错误和成功警报相同的样式
【问题讨论】:
标签: ruby-on-rails twitter-bootstrap rest alert crud