CarrierWave文件上传在Rails中不起作用 [英] CarrierWave File Uploads not working in rails

查看:85
本文介绍了CarrierWave文件上传在Rails中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像上传到我的电影收藏应用程序
i使用carrierwave来做到这一点(遵循railscasts步骤



步骤1我将gem'carrierwave','〜> 0.9'添加到我的Gemfile中,然后运行bundle
第2步:在rask db
之后安装Rails g上载器图像,然后在Rails g支架上将电影名称设置为movietype与我的电影模型中的railscasts



  attr_accessible:name ,:moviestype,:image 
mount_uploader:image,ImageUploader

在我的_form.html中。 erb

 <%= form_for @filme,:html => {:multipart => true}做| f | %> 
<%如果@ filme.errors.any? %>
< div id = error_explanation>
< h2<%=复数(@ filme.errors.count,错误)%>禁止保存该电影:< / h2>

< ul>
<%@ filme.errors.full_messages.each做| msg | %>
< li>%= msg%< / li>
<%end%>
< / ul>
< / div>
<%end%>

< div class = field>
<%= f.label:name%>< br>
<%= f.text_field:name%>
< / div>
< div class = field>
<%= f.label:电影类型%>< br>
<%= f.text_field:moviestype%>
< / div>
< div class = field>
< br>
<%= f.file_field:image%>
< / div>

< div class = actions>
<%= f.submit%>
< / div>
<%end%>

在show.html.erb

 < p id = notice><%=通知%>< / p> 

< p>
< strong>名称:< / strong>
<%= @ filme.name%>
< / p>

< p>
< strong>电影类型:< / strong>
<%= @ filme.movi​​estype%>
< / p>
<%= image_tag @ filme.image_url(:thumb)如果@ filme.image? %>

<%=链接到编辑,edit_filme_path(@filme)%> |
<%= link_to'Back',filmes_path%>

问题是它没有上传任何图像,但是电影名称和其他字段正在插入db.how我可以解决这个问题吗?需要快速帮助。

解决方案

根据错误日志,您不允许图片保存到数据库中。
在您的 FilmesController 中,您需要允许:image 作为

  def filme_params 
params.require(:filme).permit(:name,:moviestype,:image)##添加:image属性
结束


I need to upload image to my filme collection application i use carrierwave to do this (follows the railscasts steps)

step 1 i add gem 'carrierwave', '~> 0.9' to my Gemfile then run bundle step 2 rails g uploader image then rails g scaffold filmes name moviestype after rake db step 3 rails g migration add_image_to_filmes image:string and then rake db

other step are same as railscasts

in my filme modle

attr_accessible :name, :moviestype, :image
  mount_uploader :image, ImageUploader

in my _form.html.erb

<%= form_for @filme, :html => {:multipart => true} do |f| %>
  <% if @filme.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@filme.errors.count, "error") %> prohibited this filme from being saved:</h2>

      <ul>
      <% @filme.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :moviestype %><br>
    <%= f.text_field :moviestype %>
  </div>
  <div class="field">
   <br>
    <%= f.file_field :image %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

in show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @filme.name %>
</p>

<p>
  <strong>Moviestype:</strong>
  <%= @filme.moviestype %>
</p>
<%= image_tag @filme.image_url(:thumb) if @filme.image? %>

<%= link_to 'Edit', edit_filme_path(@filme) %> |
<%= link_to 'Back', filmes_path %>

problem is it didn't upload any image but film name and other fields are inserting to db.how can i fix this ?need quick help.

解决方案

As per the error logs, you have not permitted image to be saved in the database. In your FilmesController, you need to permit :image as

def filme_params
  params.require(:filme).permit(:name, :moviestype, :image) ## Add :image attribute
end

这篇关于CarrierWave文件上传在Rails中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆