我正在尝试创建一个发布表单来发布网址&图片。嵌套表单提交时没有错误,但我无法在浏览器中显示该表单的后续图像。我已经使用paperclip gem进行图片上传。
发布模型
class Post < ActiveRecord::Base
has_many :urls
has_many :photos
accepts_nested_attributes_for :urls, :photos
end
网址模型
class Url < ActiveRecord::Base
belongs_to :post
end
照片模特
class Photo < ActiveRecord::Base
belongs_to :post
has_attached_file :image, styles: { large: "600x600>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
帖子控制器
class PostsController < ApplicationController
def index
@posts = Post.all.order(created_at: :desc)
end
def show
@post = Post.find(params[:id])
@urls = @post.urls
@photos = @post.photos
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:image, :urls_attributes => [:urlattr])
end
end
show.html.erb
<% @urls.each do |url| %>
<%= url.urlattr %>
<% end %>
<% @photos.each do |photo| %>
<%= image_tag photo.image.url(:large) %>
<% end %>
数据库架构
create_table "photos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "post_id"
end
add_index "photos", ["post_id"], name: "index_photos_on_post_id"
create_table "posts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "urls", force: :cascade do |t|
t.string "urlattr"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "post_id"
end
add_index "urls", ["post_id"], name: "index_urls_on_post_id"
end
使用表单发布网址后显示的页面(在表格中输入www.example.com)&amp;图片(未显示在浏览器中)![] http://imgur.com/ct0TjHQ
你的帮助会很棒 - 谢谢!