写真をアップロードしようとしましたが、機能していないようです。多くの写真の場合
ビュー>建物>_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 %>
ビューで>建物>_タスク
<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
UPDATE 1 db テーブル
デシベル > 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
デシベル > 作成_建物
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
私はこの例を使用しています ' http://railsforum.com/viewtopic.php?id=28447 '
エラーは発生せず、写真をアップロードしてアップロードできるものは何も表示されません。追加ボタンを押しても何も作成されません。
何が間違っているのですか?