简体   繁体   English

Ruby on Rails未检测到评论字段

[英]Ruby on Rails not detecting comment field

I'm trying to build a very basic Rails app for posting images with a comment. 我正在尝试构建一个非常基本的Rails应用程序,用于发布带有注释的图像。 The comment is a required field and the image upload should only go ahead if the comments section has a value, displaying an error message otherwise. 注释是必填字段,并且仅在注释部分具有值的情况下才能继续上传图像,否则显示错误消息。 The problem I am having is that even when the comments section is filled in it still displays an error saying that it cannot be blank. 我遇到的问题是,即使在注释部分中填充了它,仍然显示错误,指出不能为空。 I have tried adding attr_accessor's with the html field names and database values but it makes no difference. 我试过用html字段名称和数据库值添加attr_accessor,但这没什么区别。

Posts model 帖子模型

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

Posts controller 帖子控制器

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 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 %> 

Server readout 服务器读数

    => 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)

Your params & form look okay 您的参数和形式看起来还可以

But I would change your strong params to be more conventional: 但我会将您的强参数更改为更常规的:

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

private

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

I found the problem. 我发现了问题。 The issue was that I had originally installed the latest version of Paperclip and then downgraded. 问题是我最初安装了最新版本的Paperclip,然后降级了。 I had added the following gem while I was searching for a solution 我在寻找解决方案时添加了以下宝石

gem 'protected_attributes'

This was causing all required fields not to register they were being filled in, such as in user sign up fields etc. Once that gem was removed it worked just fine. 这导致所有必填字段都无法注册,例如正在用户注册字段中被填充等。一旦删除了该gem,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM