0
我得到此錯誤:param is missing or the value is empty: character
。我很困惑。在某處某處form_for必須存在問題,但我無法找到它。我正在更新Users
控制器中的Character
屬性,但我認爲這應該不重要?Rails自定義路由form_for錯誤:param丟失或值爲空
參數:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"picturethings"=>{"picture"=>[#<ActionDispatch::Http::UploadedFile:0x000001100087f8 @tempfile=#<Tempfile:/var/folders/19/_vdcl1r913g6fzvk1l56x4km0000gn/T/RackMultipart20150524-4855-1eieu5j.jpeg>,
@original_filename="GOT1.jpeg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"picturethings[picture][]\"; filename=\"GOT1.jpeg\"\r\nContent-Type: image/jpeg\r\n">]},
"commit"=>"Upload pictures",
"callsign"=>"bazzer"}
的意見/用戶/ edit.html.erb
<%= form_for @character, url: update_pictures_user_path do |f| %>
<%= f.fields_for :picturethings, html: { multipart: true } do |p| %>
<%= p.label :picture %>
<%= p.file_field :picture, multiple: true, name: "picturethings[picture][]" %>
<% end %>
<%= f.submit "Upload pictures" %>
<% end %>
users_controller.rb
def edit
@character = Character.find_by(callsign: params[:callsign])
@user = @character.sociable
@picturething = @character.picturethings.build
end
def update_pictures
@character = Character.find_by(callsign: params[:callsign])
if @character.update_attributes(update_pictures_user_params)
flash[:success] = "Pictures updated"
params[:picturethings]['picture'].each do |p|
@picturething = @character.picturethings.create!(picture: p, character_id: @character.id)
end
render 'edit'
else
render 'edit'
end
end
def update_pictures_user_params
params.require(:character).permit(picturethings_attributes: [:picture])
end
character.rb
belongs_to :sociable, polymorphic: true
has_many :picturethings
accepts_nested_attributes_for :picturethings
user.rb
has_one :character, as: :sociable, dependent: :destroy
的routes.rb
patch '/users/:callsign/update_pictures', to: 'users#update_pictures', as: :update_pictures_user
謝謝papirtiger,我給你建議的修改,我現在有一個特徵參數。但是它造成了一個新問題:每次上傳文件並刷新編輯頁面時,都會出現更多'.field' div,因此頁面上現在有大約100個'p.file_field'按鈕。我無法擺脫它們。這個角色似乎有100個「相片」。但只有少數人有相應的「圖片」(我添加了<%@ character.picturethings.each do | picturething |%> <%= image_tag picturething.picture%>>來顯示圖片)。我可能不得不爲此開始一個新問題。 – Bazley
請開始一個新問題。 – max
[完成](http://stackoverflow.com/questions/30445740/rails-unwanted-instances-appearing-in-view-and-being-saved-to-the-database) – Bazley