不显示carrierwave白名单错误

时间:2016-07-20 15:36:41

标签: ruby-on-rails ruby-on-rails-4 carrierwave

在我的Rails应用程序中,我有了载波宝石来上传内容。现在我想要验证文件格式

所以我在上传器中添加了这个:

def extension_white_list
 %w(jpg jpeg gif png)
end

当我尝试上传pdf时,它不会上传pdf但是没有错误打印到屏幕上。缺少什么?

我认为页面应该返回,以便用户有机会选择不同的文件?

由于

更新 - 表单视图代码:

<%= form_for(@channel, :html => {:multipart => true, :class => "form-horizontal", :role => "form"}, :method => :post, :url => url_for(controller: 'channels', action: 'edit', id: @channel.id)) do |f| %>
      <div class="col-md-4">
        <div class="form-group col-md-12">
          <label><%= f.label :channelimage %></label>
          <%= f.file_field :channelimage, :class => "form-control", :placeholder => :image%>
          <br>
          <% if @channel.channelimage.present? %>
              <div clasS="thumbnail">
                <img src="<%= @channel.channelimage %>" alt="<%= @channel.channelname %>"/>
              </div>
          <% end %>
        </div>
      </div>

更新:控制器功能

#Speichert die geänderten Werte eines Channels
def edit
  @user = User.find_by_id session[:user_id]
  @knowledgeproviderList = @user.knowledgeprovider
  @channel = Channel.find params[:id]
  @channelList = Channel.where(knowledgeprovider_id: @knowledgeproviderList.pluck(:id))
  if request.post?
    @channel.update_attributes(channel_edit_params)
    if @channel.save
      flash[:notice] = t("flash.saved")
      redirect_to action: :index
    else
      redirect_to action: :edit, :id => @channel.id
    end
  end
end

1 个答案:

答案 0 :(得分:1)

Validation - Guides

所以基本上你没有输出错误,所以使用你需要添加的指南中的格式:

<% if @channel.errors.any? %>
  <div id="error_explanation">
    <h2>
      <%= pluralize(@channel.errors.count, "error") %> prohibited
      this channel from being saved:
    </h2>
    <ul>
      <% @channel.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>