我尝试上传照片,但似乎不起作用。很多照片
在视图>建筑>_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 %>
在视图>建筑>_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
在控制器>建筑物中
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
在视图>布局>应用程序中
<!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>
模型>任务
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
模型>建筑
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 个数据库表
数据库>创建任务
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :name
t.integer :building_id
t.timestamps
end
end
end
数据库> 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 回形针建筑照片
现在在数据库中我有
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
我正在使用这个例子' http://railsforum.com/viewtopic.php?id=28447 '
我没有收到任何错误,也看不到任何可以上传照片的内容。当我按下添加按钮时,什么也没有做。
有什么想法有什么问题吗?