【问题标题】:Ruby on Rails not detecting comment fieldRuby on Rails 未检测到评论字段
【发布时间】:2014-04-23 01:08:50
【问题描述】:

我正在尝试构建一个非常基本的 Rails 应用程序来发布带有评论的图像。注释是必填字段,只有当 cmets 部分有值时,才应继续上传图片,否则显示错误消息。 我遇到的问题是,即使填写了 cmets 部分,它仍然会显示一个错误,指出它不能为空白。 我尝试使用 html 字段名称和数据库值添加 attr_accessor,但没有区别。

帖子模型

class Post < ActiveRecord::Base
  has_attached_file :picture, styles: { medium: "300x300>", thumb: "100x100>" }
  attr_accessor :picture_file_name
  attr_accessor :post_description

  validates :description, presence: true
end

帖子控制器

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new params[:post].permit(:description, :picture)

    if @post.save
      redirect_to '/posts'
    else
      render 'new'
    end
  end
end

new.html.erb

 <% if @post.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being     saved:</h2>

    <ul>
    <% @post.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<%= form_for @post, :html => { :multipart => true } do |f| %>
  <%= f.label :description %>
  <%= f.text_area :description %>

  <%= f.label :picture %>
  <%= f.file_field :picture %>

  <%= f.submit %>
<% end %> 

服务器读数

    => Booting WEBrick
=> Rails 4.0.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-04-20 17:59:07] INFO  WEBrick 1.3.1
[2014-04-20 17:59:07] INFO  ruby 2.1.0 (2013-12-25) [x86_64-darwin12.0]
[2014-04-20 17:59:07] INFO  WEBrick::HTTPServer#start: pid=98772 port=3000


Started POST "/posts" for 127.0.0.1 at 2014-04-20 17:59:21 +0100
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"nVrCEAdT+epbltQWR74jtv1weGaq6H7YbWQKFfJNDTw=", "post"=>{"description"=>"test comment", "picture"=>#<ActionDispatch::Http::UploadedFile:0x0000010242f740 @tempfile=#<Tempfile:/var/folders/lm/vrw53rx91831vrh4228m0mfw0000gn/T/RackMultipart20140420-98772-1c9msrz>, @original_filename="dory_2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[picture]\"; filename=\"dory_2.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Post"}
WARNING: Can't mass-assign protected attributes for Post: description, picture
    app/controllers/posts_controller.rb:12:in `create'
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendered posts/new.html.erb within layouts/application (13.1ms)
Completed 200 OK in 105ms (Views: 65.6ms | ActiveRecord: 0.4ms)

【问题讨论】:

  • 听起来您没有通过表单发送描述?你确定params[:post] 包含图片和描述吗? (尝试打印它只是为了仔细检查)
  • 不知道你的意思是什么对不起。您的意思是将评论打印为 HTML 的一部分?
  • 将我的 new.html.erb 添加到帖子中。包含描述和图片
  • 看起来不错,我的意思是打印您的参数,例如 p params[:post].permit(:description, :picture),以检查您是否收到了您认为应该收到的信息,(您可以查看您的服务器日志以查看打印的内容)
  • 另外,仅供参考,更标准的语法是params.require(:post).permit(:description, :picture)

标签: html ruby-on-rails ruby image-uploading


【解决方案1】:

你的参数和表单看起来不错

但我会将您的 strong params 更改为更传统:

def create
    @post = Post.new(post_params)
    @post.save
end

private

def post_params
    params.require(:post).permit(:description, :picture)
end

【讨论】:

    【解决方案2】:

    我发现了问题。 问题是我最初安装了最新版本的 Paperclip,然后降级了。 我在寻找解决方案时添加了以下 gem

    gem 'protected_attributes'
    

    这导致所有必填字段未注册它们正在填写的内容,例如用户注册字段等。 一旦移除该宝石,它就可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多