简体   繁体   English

Rails:要上传的文件不会从Form传递到控制器

[英]Rails: File to Upload does not get passed from Form to the controller

This is the Form. 这是表格。 All of the fields get passed (and saved) except the one containing the File. 除包含“文件”的字段外,所有字段都将被传递(并保存)。 I have checked that using the 我已经检查过使用

render plain: params[:article].inspect   method

giving out this (I have entered the value "n" for all fields): 给出这个(我在所有字段中输入了值“ n”):

{"country"=>"n", "region"=>"n", "town"=>"n", "street"=>"n", "company"=>"n", "title"=>"n", "content"=>"n"}

I am leaving out superfluous fields here to make the Form shorter: 我在这里省略了多余的字段,以使表格更简短:

<%= form_for(@article, html: { multipart: true }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>       
          <%= f.label :country %>
          <%= f.text_field :country, :required => true, 
          placeholder: "enter country" %>
          </div>                  
      <%= f.label :content %>
      <%= f.text_field :content, :required => true, placeholder: "town..." %>
            </div>  
         </div>
     </div>         
  </div>       
<span class="picture">
   <%= form_for @article, html: { multipart: true } do |f| %>
     <%= f.text_field :title %>
     <%= fields_for :pictures do |ff| %>
     <%= ff.file_field :picture %>
   <% end %>
  <% end %> 
</div>

I have also tried the slight variation here, but no change 我也在这里尝试了细微的变化,但没有变化

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

The create method at the Controller is like this: Controller上的create方法如下:

def create
      @article = current_user.articles.build(article_params)
    if @article.save
      flash[:success] = "Article created!"
      redirect_to root_url
    else
      render 'articles/new'
    end
  end

and yes, the new method in the Articles controller, is like I was indicated by peers here: 是的,Articles控制器中的新方法就像我的同伴在这里指出的那样:

def new
    @article  = current_user.articles.build 
    @article.pictures.build
  end

The Article Model 文章模型

class Article < ActiveRecord::Base
  belongs_to :user
  has_many :pictures
  accepts_nested_attributes_for :pictures, allow_destroy: true

And the pictures Model 和图片模型

class Picture < ActiveRecord::Base
  belongs_to :article
 mount_uploader :picture, PictureUploader

end

Change your <%= fields_for :pictures do |ff| %> 更改您的<%= fields_for :pictures do |ff| %> <%= fields_for :pictures do |ff| %> to <%= f.fields_for :pictures do |ff| %> <%= fields_for :pictures do |ff| %><%= f.fields_for :pictures do |ff| %> <%= f.fields_for :pictures do |ff| %>

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

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