我尝试上传照片似乎没有工作。 FOR MANY PHOTOS
在视图> building> _form
中<%= form_for(@building, :html=>{:multipart => true}) do |f| %>
<% if @building.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@building.errors.count, "error") %> prohibited this building from being saved:</h2>
<ul>
<% @building.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.select :status, Building::STATUS, :prompt=>'Select status of the building' %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 10 %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<h3>Tasks</h3>
<% f.fields_for :tasks do |task_form| -%>
<%= render :partial => 'task', :locals => { :form => task_form } %>
<% end -%>
<%= add_photo(f) %>
<%= f.file_field :foto%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
在视图中&gt;构建&gt; _task
<div class="task">
<p>
<%= form.label :name %>
<%= form.text_field :name, :size => 15 %>
<%= remove_task_link( form ) %>
</p>
</div>
in helpers>building_helpers
module BuildingsHelper
def add_photo(form_builder)
link_to_function "add", :id => "add_photo" do |page|
form_builder.fields_for :tasks, Task.new, :child_index => 'NEW_RECORD' do |f|
html = render(:partial => 'task', :locals => { :form => f })
page << "$('tasks').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });"
end
end
end
def remove_task_link(form_builder)
if form_builder.object.new_record?
# If the task is a new record, we can just remove the div from the dom
link_to_function("remove", "$(this).up('.task').remove();");
else
# However if it's a "real" record it has to be deleted from the database,
# for this reason the new fields_for, accept_nested_attributes helpers give us _delete,
# a virtual attribute that tells rails to delete the child record.
form_builder.hidden_field(:_delete) +
link_to_function("remove", "$(this).up('.task').hide(); $(this).previous().value = '1'")
end
end
end
在控制器&gt;建筑物
中 def new
@building = Building.new
2.times { @building.tasks.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @building }
end
end
# GET /buildings/1/edit
def edit
@building = Building.find(params[:id])
2.times { @building.tasks.build }
end
在视图&gt;布局&gt;应用
中<!DOCTYPE html>
<html>
<head>
<title>Welcome to koshbay</title>
<%= stylesheet_link_tag :all %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
模型&GT;任务
class Task < ActiveRecord::Base
attr_accessible :building_id, :name
belongs_to :project
has_attached_file :foto, :styles => { :medium => "300x300>",
:thumb => "100x100>" ,
:default_url => "/images/:style/missing.png"}
end
模型&GT;建立
class Building < ActiveRecord::Base
attr_accessible :description, :price, :status, :title ,:location, `:foto`
has_many :tasks, :dependent => :destroy
# This is new!
accepts_nested_attributes_for :tasks, :allow_destroy => true
end
更新1个数据库表
分贝&GT; create_task
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :name
t.integer :building_id
t.timestamps
end
end
end
分贝&GT; create_buildings
class CreateBuildings < ActiveRecord::Migration
def change
create_table :buildings do |t|
t.string :title
t.string :location
t.string :status
t.text :description
t.decimal :price, :precision=>8, :scale => 2
t.timestamps
end
end
end
更新2
问题出在_task.html.erb
更新3
我已经运行了rails g paperclip building foto
现在在db我有
class AddAttachmentFotoToBuildings < ActiveRecord::Migration
def self.up
change_table :buildings do |t|
t.has_attached_file :foto
end
end
def self.down
drop_attached_file :buildings, :foto
end
end
我正在使用这个例子 &#39; http://railsforum.com/viewtopic.php?id=28447&#39;
我没有得到任何错误,我无法看到任何我可以把照片上传它。当我按下添加按钮没有做任何事情。
任何想法有什么不对?
答案 0 :(得分:1)
你可以在这里看到你的项目:
<强> https://github.com/DamirSvrtan/For-mario 强>
你可以从那里克隆它。无论如何,给你详细信息:
1.您未将此行添加到config/environments/development.rb:
Paperclip.options[:command_path] = "/usr/local/bin/"
你可以看到我把那条线放在那里,它实际上是"/usr/bin"
运行哪个转换命令,获得的内容,然后从中删除最后一个'/convert'
并将其放在development.rb
文件中。
2.您的建筑模型中有foto属性,但您的任务模型中有has_attached。我也修好了。我还改变了你的视图并删除了嵌套属性来处理照片,而我已经制作了建筑形式来处理它。 我不小心擦除了表单中的嵌套属性部分,所以只需将其取回,我认为有两行。
3.由于某种原因,您的代码不能与has_attachment的“styles”部分一起使用。我真的不知道为什么会这样,因为我的所有人到目前为止都完美地工作了。 我试图解决它,但我找不到解决方案。其他一些人在stackoverflow上也有同样的问题,也许它是一些ruby-rails版本的东西。我没有其他任何想法。如果你没有找到解决方案,你仍然可以“呼吸”它,但是,也许再问一下这里为什么会这样。
4.我将回形针宝石改为:
gem "paperclip", "~> 3.0"
5.希望你能理解我所做的所有改变。您可以接受代码,也可以不接受。
答案 1 :(得分:0)
好的,这就是我使用嵌套属性所做的工作。 要做到这一点,你需要2插入1)paperclip 2)imagemagick
宝石文件中的添加行
gem'paperclip'
然后在cmd:bundle install
中运行然后运行gem install rmagick //&lt; -------我会工作的。我忘了
一步步骤
** p.s:建筑是关系一(建筑物)到(很多)照片**
步骤1:我用commant创建一个表:rails g model buildingphoto building_id:integer
分贝
class CreateBuildingPhotos < ActiveRecord::Migration
def change
create_table :building_photos do |t|
t.integer :building_id
t.timestamps
end
end
end
第2步:commant&gt; rails g paperclip buildingphoto photo
分贝
class AddAttachmentPhotoToBuildingPhotos < ActiveRecord::Migration
def self.up
change_table :building_photos do |t|
t.has_attached_file :photo
end
end
def self.down
drop_attached_file :building_photos, :photo
end
end
完成第2步后:rake db:migrate
in model&gt; buildingphoto
class BuildingPhoto < ActiveRecord::Base
attr_accessible :building_id , :photo
belongs_to :building
has_many :photo
has_attached_file :photo
end
在模型&gt;建筑
中class Building < ActiveRecord::Base
attr_accessible :building_photos_attributes
has_many :building_photos, :dependent => :destroy
accepts_nested_attributes_for :building_photos
end
控制器&gt; buildingcontroller中的
def new
@building = Building.new
4.times { @building.building_photos.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @building }
end
end
在视图中&gt;构建&gt; _buildingphoto.html.erb //我创建它
<%= f.label :photo %><br />
<%= f.file_field :photo %>
在视图中&gt;构建&gt; _form.html.erb //我添加此行
<div class="field">
<%= f.fields_for :building_photos do |builder| %>
<%= render "buildingphoto", :f => builder %>
<% end %>
</div>
在config&gt;环境中&gt; development.rb //告诉Paperclip在哪里找到您的ImageMagick安装(并非总是需要)
Paperclip.options[:command_path] = "/usr/bin/"
查看您上传的照片
<% building.building_photos.each do |photo| %>
<%= link_to (image_tag (photo.photo.url)), building %>
<% end %>