简体   繁体   中英

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

{"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

The create method at the Controller is like this:

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:

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| %> to <%= f.fields_for :pictures do |ff| %> <%= f.fields_for :pictures do |ff| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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