【发布时间】:2015-10-04 17:07:05
【问题描述】:
在我的用户上传文件并点击“提交”后,我想提醒他们上传成功。但是当我这样做时,什么都没有发生。
这是在控制器中:
def create
# make a new picture with what picture_params returns (which is a method we're calling)
@picture = Picture.new(picture_params)
if @picture.save
# if the save for the picture was successful, go to index.html.erb
redirect_to pictures_url
flash[:alert] = "Upload successful!"
else
# otherwise render the view associated with the action :new (i.e. new.html.erb)
render :new
end
end
表格:
<container>
<center>
<%= form_for @picture do |f| %>
<input type="file" multiple> <%= f.file_field :picture %>
<p>Drag your files here or click in this area.</p>
<button type="submit"> <%= f.submit "Save" %> Upload </button>
<% if flash[:alert] %>
<div class="alert"><%= flash[:alert] %></div>
<% end %>
</form>
<% end %>
</container>
谢谢!
【问题讨论】:
-
因为你在设置
flash[:alert]之前重定向(相当于return) -
我把它们换了,还是不行?
-
我认为
redirect_to不会返回并停止执行它之后的其余行。你图片的index.html.erb中有flash[:alert]。图片保存成功后,您尝试在其中显示flash[:alert]的表单不会呈现。你应该检查你的索引文件。
标签: ruby-on-rails ruby upload alert