我有一个用于创建项目的表单,该项目可以包含五张照片。
在我的_form.html.erb中,当我打电话给multipart => true
时,我无法访问我的上传照片,但在我的节目中,他们可以访问。如果有任何现有照片,我想在edit.erb和删除选项中显示现有照片。我可以在节目中访问上传的照片。
这是我的_form.html.erb:
<%= form_for @project, :html => { :multipart => true } do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.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 :description %><br>
<%= f.text_area :description %>
</div>
<div class="newPaperclipFiles">
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :photo %>
<% end %>
<% end %>
</div>
<div class="existingPaperclipFiles">
<% f.fields_for :assets do |asset| %>
<% unless asset.object.new_record? %>
<div>
<%=image_tag asset.photo.url(:small) %>
</div>
<%= asset.check_box :_destroy %>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我可以访问照片的show.erb:
<div class="existingPaperclipFiles">
<% @project.assets.each do |asset| %>
<div>
<%= image_tag asset.photo.url(:small) %>
</div>
<% end %>
</div>
<p>
<strong>Title:</strong>
<%= @project.title %>
</p>
<p>
<strong>Description:</strong>
<%= @project.description %>
</p>
<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>
我的资产模型:
class Asset < ActiveRecord::Base
require 'paperclip'
belongs_to :project, :foreign_key => "project_id"
attr_accessible :project_id, :photo
has_attached_file :photo, :styles => { :thumb => "600x600#", :medium => "300x300#", :small => "160x160#"}
end
我的项目模型:
class Project < ActiveRecord::Base
has_permalink :title
default_scope :order => 'created_at desc'
attr_accessible :title, :description, :assets_attributes, :dependent => :destroy
validates_uniqueness_of :title
validates_presence_of :title
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
end
编辑:
项目控制器:
class ProjectsController < ApplicationController
before_filter :authenticate_admin!, :except => [:show]
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new()
(5 - @project.assets.length).times { @project.assets.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find_by_permalink(params[:id])
@assets = Project.includes(:assets).find_by_permalink(params[:id])
(5 - @project.assets.length).times { @project.assets.build }
end
# POST /projects
# POST /projects.json
def create
@project = Project.create(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render action: 'show', status: :created, location: @project }
else
format.html { render action: 'new' }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
@project = Project.find_by_permalink(params[:id])
respond_to do |format|
if @project.update(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project = Project.find_by_permalink(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find_by_permalink(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:title, :description, :assets_attributes)
end
end
答案 0 :(得分:0)
不确定这是否是拼写错误,但请尝试在此处添加=
:
<% f.fields_for :assets do |asset| %>
<%= f.fields_for :assets do |asset| %>