我正在尝试通过ImageUploader上传与每个帖子相关联的图片。但是,在选择图像文件并点击上传按钮后,没有文件URL保存到数据库中。我已在rails控制台中检查了图像网址,但图像未保存。我找到帖子并查看与其关联的所有其他信息和图片:nil。提交帖子创建表单时,我也没有收到任何错误。但是,当页面刷新时,选择并“保存”的图像消失。我已正确安装了carrierwave,mini_magick和雾。有关图像未上传的任何想法?谢谢大家。
到目前为止,这是我的代码:
发布模型:
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
mount_uploader :image, ImageUploader
default_scope { order('created_at DESC')}
validates :title, length: {minimum: 5}, presence: true
validates :body, length: {minimum: 20}, presence: true
validates :topic, presence: true
validates :user, presence: true
def markdown_title
render_as_markdown(self.title)
end
def markdown_body
render_as_markdown(self.body)
end
private
def render_as_markdown(markdown)
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_block: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
(redcarpet.render markdown).html_safe
end
end
app / uploaders / image_uploader.rb上传路径:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
发布_form文件:
<%= form_for [topic, post] do |f| %>
<% if post.errors.any? %>
<div class="alert alert-danger">
<h4>There are <%= pluralize(post.errors.count, "error") %>.</h4>
<ul>
<% post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_group_tag(post.errors[:title]) do %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
<% end %>
<%= form_group_tag(post.errors[:body]) do %>
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.hidden_field :image %>
</div>
<div class="form-group">
<%= f.submit "Save", class: "btn btn-success" %>
</div>
<% end %>
发布/编辑文件:
<h1>Edit Post</h1>
<div class="row">
<div class="col-md-4">
<p>Guidelines for posts</p>
<ul>
<li>Make sure it rhymes.</li>
<li>Don't use the letter "A".</li>
<li>The incessant use of hashtags will get you banned.</li>
</ul>
</div>
<div class="col-md-8">
<%= render partial: 'form', locals: { topic: @topic, post: @post } %>
</div>
</div>
发布/显示文件:
<h1><%= @post.markdown_title %></h1>
<div class="row">
<div class="col-md-8">
<small>
<%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
submitted <%= time_ago_in_words(@post.created_at) %> ago by
<%= @post.user.name %>
</small>
<p><%= @post.markdown_body %></p>
<small>
<%= image_tag @post.image_url%>
</small>
</div>
<div class="col-md-4">
<% if policy(@post).edit? %>
<%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
<% end %>
</div>
发布控制器:
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
authorize @post
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.user = current_user
@post.topic = @topic
authorize @post
if @post.save
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving this post. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
if @post.update_attributes(post_params)
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
private
def post_params
params.require(:post).permit(:title, :body, :image)
end
end
服务器登录提交表单:
Started GET "/topics/1/posts/61/edit" for ::1 at 2015-08-02 21:49:54 -0400
Processing by PostsController#edit as HTML
Parameters: {"topic_id"=>"1", "id"=>"61"}
Topic Load (0.1ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1 [["id", 1]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 61]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 6]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Rendered posts/_form.html.erb (2.3ms)
Rendered posts/edit.html.erb within layouts/application (4.1ms)
Completed 200 OK in 79ms (Views: 76.3ms | ActiveRecord: 0.3ms)
Started PATCH "/topics/1/posts/61" for ::1 at 2015-08-02 21:50:01 -0400
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WrE4W30KlADBMbzSfNe/BCQPEHL3M4NhPGEo+Eop0iB9DA8QSHfz0zyBXsuW1NIHSyR7vFU1M7Ny4wUFMXO/Hg==", "post"=>{"title"=>"IMAGE IMAGE TESTING", "body"=>"JUST DO IT!!!!!! IMAGEEEEEEEE", "image"=>""}, "commit"=>"Save", "topic_id"=>"1", "id"=>"61"}
Topic Load (0.1ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1 [["id", 1]]
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 61]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 6]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
(0.1ms) begin transaction
Topic Load (0.1ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1 [["id", 1]]
Post Load (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 61]]
SQL (23.1ms) UPDATE "posts" SET "image" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["image", nil], ["updated_at", "2015-08-03 01:50:01.161743"], ["id", 61]]
(1.4ms) commit transaction
Redirected to http://localhost:3000/topics/1/posts/61
Completed 302 Found in 33ms (ActiveRecord: 25.0ms)
Started GET "/topics/1/posts/61" for ::1 at 2015-08-02 21:50:01 -0400
Processing by PostsController#show as HTML
Parameters: {"topic_id"=>"1", "id"=>"61"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 61]]
Topic Load (0.1ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 6]]
Rendered posts/show.html.erb within layouts/application (2.8ms)
Completed 200 OK in 73ms (Views: 71.4ms | ActiveRecord: 0.4ms)