[英]Rails: File to Upload does not get passed from Form to the controller
這是表格。 除包含“文件”的字段外,所有字段都將被傳遞(並保存)。 我已經檢查過使用
render plain: params[:article].inspect method
給出這個(我在所有字段中輸入了值“ n”):
{"country"=>"n", "region"=>"n", "town"=>"n", "street"=>"n", "company"=>"n", "title"=>"n", "content"=>"n"}
我在這里省略了多余的字段,以使表格更簡短:
<%= 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>
我也在這里嘗試了細微的變化,但沒有變化
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
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
是的,Articles控制器中的新方法就像我的同伴在這里指出的那樣:
def new
@article = current_user.articles.build
@article.pictures.build
end
文章模型
class Article < ActiveRecord::Base
belongs_to :user
has_many :pictures
accepts_nested_attributes_for :pictures, allow_destroy: true
和圖片模型
class Picture < ActiveRecord::Base
belongs_to :article
mount_uploader :picture, PictureUploader
end
更改您的<%= 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.