这是视图
<%= '<br /><br />' if flash %>
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : name.to_s %>">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg.html_safe, :id => "flash_#{name}" if msg.is_a?(String) %>
</div>
<% end %>
<%= yield %>
当我删除一条记录后,下面的代码仅在屏幕顶部显示一条闪烁消息(“已删除”)。那这就是我想要的。
def update
if params[:destroy]
if current_user.id == @code.user_id
@code.destroy
flash[:notice] = "Deleted"
else
flash[:notice] = "You don't have permission to edit"
end
respond_to do |format|
format.html { redirect_to community_codes_url }
format.json { head :no_content }
end
else
respond_to do |format|
if @code.update_attributes(params[:code])
format.html { redirect_to community_code_path(@community.community_name, @code), notice: 'Updated' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @code.errors, status: :unprocessable_entity }
end
end
end
end
但是下面的这段代码搞砸了这条规则,我完全糊涂了:(
如果我创建新记录,它应该只显示一条像以前一样的闪存消息。然后它里面有推特按钮。
此 flash 不仅包含字符串,还包含html
和包含javascript
在 flash 消息中。
我认为是否会有 2 条闪现消息并不重要:(
如何只显示一条闪信息?
def create
@code = @community.codes.build (params[:code])
@code.user_id = current_user.id
respond_to do |format|
if @code.save
tag_strings = @community.tags.join(", ") + "," if @community.tags
flash[:notice] = '<a href="https://twitter.com/share" class="twitter-share-button" data-lang="en" data-hashtags="' + tag_strings + 'tags here!">Tweet</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>You can tweet if you click this button'
format.html { redirect_to community_code_path(@community.community_name, @code) }
format.json { render json: [@community, @code], status: :created, location: @code }
else
format.html { render action: "new" }
format.json { render json: @code.errors, status: :unprocessable_entity }
end
end
end