2015-05-24 51 views
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 

回答

0

您的文件輸入名稱不正確。如果你看錶​​格數據:

form-data; name=\"picturethings[picture][]\"; filename=\"GOT1.jpeg\"\r\nContent-Type: image/jpeg\r\n" 

沒有character參數。

正確的name屬性會是這個樣子:

character[picturethings_attributes][1][picture][] 

當Rails的解析表單數據,將其轉換成以下散列:

character: { 
    picturethings_attributes: { 
    0 => { 
     picture : [] # an array of files. 
    } 
    } 
} 

你並不需要指定名稱手動:

<%= 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 %> 
    <% end %> 
    <%= f.submit "Upload pictures" %> 
<% end %> 

Rails會給你正確的名稱屬性。你也不需要手動創建相關的記錄 - 這是的accepts_nested_attributes_for整點:

def update_pictures 
    @character = Character.find_by(callsign: params[:callsign]) 
    if @character.update_attributes(update_pictures_user_params) 
    flash[:success] = "Pictures updated" 
    render 'edit' 
    else 
    render 'edit' 
    end 
end 
+0

謝謝papirtiger,我給你建議的修改,我現在有一個特徵參數。但是它造成了一個新問題:每次上傳文件並刷新編輯頁面時,都會出現更多'.field' div,因此頁面上現在有大約100個'p.file_field'按鈕。我無法擺脫它們。這個角色似乎有100個「相片」。但只有少數人有相應的「圖片」(我添加了<%@ character.picturethings.each do | picturething |%> <%= image_tag picturething.picture%>>來顯示圖片)。我可能不得不爲此開始一個新問題。 – Bazley

+0

請開始一個新問題。 – max

+0

[完成](http://stackoverflow.com/questions/30445740/rails-unwanted-instances-appearing-in-view-and-being-saved-to-the-database) – Bazley

相關問題