首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在rails中的图像模型中,Params没有出现错误

问在rails中的图像模型中,Params没有出现错误
EN

Stack Overflow用户
提问于 2016-05-17 14:12:11
回答 1查看 129关注 0票数 0

我有一个picture模型,它使用carrierwave上传文件。picture模型是user模型和scoreboard模型的多态关系。所有相关代码如下所示。

可显示控制器

代码语言:javascript
复制
class PicturesController < ApplicationController        
  before_filter :load_pictureable
  before_action :logged_in_user, only: [:create, :update, :destroy]

  def create
    @picture = @pictureable.build_picture(picture_params)
    if @picture.save
        flash[:success] = "Uploaded Successfully"
        redirect_to @pictureable
    else
        redirect_to @pictureable
        flash[:danger] = "An error occured, please try again!"
    end
  end

  def update
    @picture = @pictureable.picture
    if @picture.update_attributes(picture_params)
       redirect_to @pictureable
       flash[:success] = "Picture updated successfully"
     else
       redirect_to @pictureable
       flash[:danger] = "An error occured, please try again!"
     end
  end

  def destroy
    @pictureable.picture.delete
    redirect_to @pictureable
    flash[:success] = "Picture removed successfully"
  end

private

  def picture_params
    params.require(:picture).permit(:picture)
  end

  def load_pictureable
    resource, id = request.path.split('/')[1,2]
    @pictureable = resource.singularize.classify.constantize.find(id)
  end
end

我在前端使用HTML5电子阅读器Javascript以一个模式读取图像。文件在模式中显示得很好。Javascript代码如下所示。

代码语言:javascript
复制
$(document).ready(function(){
    var preview = $(".upload-preview img");

    $(".hidden").change(function(event){
       var input = $(event.currentTarget);
       var file = input[0].files[0];
       var reader = new FileReader();
       reader.onload = function(e){
           $('#image')
                    .attr('src', e.target.result)
                    .width(307)
                    .height(317);
           image_base64 = e.target.result;
           preview.attr("src", image_base64);
       };
       reader.readAsDataURL(file);
    });
});

create的动作工作得很完美。但是,当我试图更新一个现有图片时,它是,有时会给我以下错误:param is missing or the value is empty: picture.错误只是偶尔发生。我不太清楚到底发生了什么。我知道错误告诉我picture param不存在。

记录不成功修补程序请求的日志文件。

代码语言:javascript
复制
Started PATCH "/scoreboards/7/pictures/24" for 70.26.106.141 at 2016-05-17 16:27:32 +0000
Processing by PicturesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WoA40xzlc8CH6/gcf6Jd/nqlj/9iOoykcn4H9zAgTkuwi6vAttZEPECYs+OfHZvC3le3nY/sZ/IZSwOL0FhnIQ==", "scoreboard_id"=>"7", "id"=>"24"}
  [1m[36mScoreboard Load (0.4ms)[0m  [1mSELECT  "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = $1  ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m  [["id", 7]]
  [1m[35mUser Load (0.3ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mPicture Load (14.4ms)[0m  [1mSELECT  "pictures".* FROM "pictures" WHERE "pictures"."pictureable_id" = $1 AND "pictures"."pictureable_type" = $2 LIMIT 1[0m  [["pictureable_id", 7], ["pictureable_type", "Scoreboard"]]
Completed 400 Bad Request in 24ms

ActionController::ParameterMissing (param is missing or the value is empty: picture):
  app/controllers/pictures_controller.rb:40:in `picture_params'
  app/controllers/pictures_controller.rb:20:in `update'


  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (5.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (2.3ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.5ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (1.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (110.5ms)

成功的POST请求的日志文件

代码语言:javascript
复制
Started POST "/scoreboards/7/pictures" for 70.26.106.141 at 2016-05-17 16:30:00 +0000
Processing by PicturesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jNhAeqUJNZGKEGdDCWSGzGRBSwyXkQIj9iZgMMklqBpm09NpDzoCbU1jLLzp20DwwLNzbnpH6XWdE2RMKV2BcA==", "picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x00000006c6b7d0 @tempfile=#<Tempfile:/tmp/RackMultipart20160517-27824-1d2x6l4.jpeg>, @original_filename="Home_pic.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[picture]\"; filename=\"Home_pic.jpeg\"\r\nContent-Type: image/jpeg\r\n">}, "scoreboard_id"=>"7"}
  [1m[35mScoreboard Load (0.4ms)[0m  SELECT  "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = $1  ORDER BY "scoreboards"."created_at" DESC LIMIT 1  [["id", 7]]
  [1m[36mUser Load (0.3ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mPicture Load (0.5ms)[0m  SELECT  "pictures".* FROM "pictures" WHERE "pictures"."pictureable_id" = $1 AND "pictures"."pictureable_type" = $2 LIMIT 1  [["pictureable_id", 7], ["pictureable_type", "Scoreboard"]]
  [1m[36m (0.2ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.7ms)[0m  INSERT INTO "pictures" ("picture", "pictureable_id", "pictureable_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["picture", "Home_pic.jpeg"], ["pictureable_id", 7], ["pictureable_type", "Scoreboard"], ["created_at", "2016-05-17 16:30:03.046590"], ["updated_at", "2016-05-17 16:30:03.046590"]]
  [1m[36m (29.2ms)[0m  [1mCOMMIT[0m
Redirected to https://scorecliq-kpauls.c9users.io/scoreboards/7
Completed 302 Found in 2253ms (ActiveRecord: 31.2ms)

视图文件在模式中被处理。代码如下所示。editphoto模式和upload photo模式是相同的。下面的是EDITPHOTO模式/上传照片.

代码语言:javascript
复制
<div>
  <%= form_for([@pictureable, @picture], html: {multipart: true}) do |f| %>
   <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png', id: "files", class: "hidden" %>

     <div class="modal fade" id="picmodal" tabindex="-1" role="dialog" aria-labelledby="picmodal" aria-hidden="true"> 
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header" style="text-align: center">Selected Image</div>
             <div class="modal-body">
             <div class="upload-preview" style="text-align: center">
               <img id="image" /></img>
             </div>
            </div>
            <div class="modal-footer">
                <%= f.submit "upload photo", class: "hidden", id: "editphotobutton" %>
                <button class="btn-s btn-full", for: "editphotobutton">Save Changes</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>
  <% end %>
</div>

单击选项以更改图片的视图代码

代码语言:javascript
复制
   <div class="col-xs-12 col-sm-3 score-prof-pic">
         <% if @picture.picture.url.present? %>
            <div class="dropdown">
               <%= link_to "#", class: "prof-dropdown" , data: {:toggle => "dropdown"}, role: "button", aria: {:expanded => "false"} do %>
                   <%= image_tag @picture.picture.url, class: "prof-default img-thumbnail" %>
               <% end %>
               <% if manager_or_owner?(@scoreboard, current_user) %>
                  <ul class="dropdown-menu">
                     <li><%= link_to "Upload","#", id: "score-prof-edit" %> </li>
                     <li role="separator" class="divider"></li>
                     <li> <%= link_to "Remove", [@pictureable, @picture], method: :delete, data: {confirm: "Are you sure you want to delete your picture!"} %></li>
                     <li role="separator" class="divider"></li>
                     <li><%= link_to "Cancel", "#" %></li>
                  </ul>
               <% end %>
            </div>
         <% else %>
            <div class="dropdown">
               <%= link_to "#", class: "prof-dropdown" , data: {:toggle => "dropdown"}, role: "button", aria: {:expanded => "false"} do %>
                   <%= image_tag "blank-prof.jpg", class: "prof-default img-thumbnail " %>
               <% end %>
               <% if manager_or_owner?(@scoreboard, current_user) %>
               <ul class="dropdown-menu">
                  <li><%= link_to "Upload","#", id: "score-prof-upload" %> </li>
                  <li role="separator" class="divider"></li>
                  <li><%= link_to "Cancel", "#" %></li>
               </ul>
               <% end %>
            </div>
         <% end %>
     </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-18 05:40:44

控制器的Rails强参数当前要求在用户更新图片模型时上传新图片。这意味着,如果用户提交表单而没有上传新图像,即使他们以前成功地上传了图像,强参数也会导致操作失败。

若要解决此问题,请将picture_params方法更改为:

代码语言:javascript
复制
def picture_params
  params.permit(:picture)
end

这将允许update工作,即使用户没有选择新文件。理想情况下,UI将禁用Save或Submit按钮,直到用户选择要上载的文件,以确保良好的用户体验。

若要验证用户至少上传了一次文件,请向图片模型中添加验证,以验证用户是否存在图片。在不知道字段名的具体细节的情况下,您可以像这样进行验证(假设:image:

代码语言:javascript
复制
def Picture < ActiveRecord::Base
  validate :has_picture

private

  def has_picture
    errors.add(:picture, "can't be missing") if !picture.url.present?
  end
end

这种方法将在节省时间(而不是在调用操作时)为您提供验证。这不仅应使操作更加健壮,而且应确保模型始终有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37278627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档