创建/更新与上载图像相关的对象时缺少参数

时间:2012-08-23 19:21:43

标签: ruby-on-rails paperclip

编辑:将错误缩小到我尝试通过回形针上传图像时

好的,所以只是尝试添加/编辑我的数据库的元素,这些元素在heroku上启动并开始出错。检查了heroku日志得到了这个

2012-08-23T19:10:42+00:00 app[web.1]: Completed 500 Internal Server Error in 363ms
2012-08-23T19:10:42+00:00 app[web.1]: 
2012-08-23T19:10:42+00:00 app[web.1]: NoMethodError (undefined method `[]' for nil:NilClass):
2012-08-23T19:10:42+00:00 app[web.1]:   app/controllers/apartments_controller.rb:68:in `new'
2012-08-23T19:10:42+00:00 app[web.1]:   app/controllers/apartments_controller.rb:68:in `create'

如果我查看apt控制器第68行的内容,我就明白了......

def create
@apartment = Apartment.new(params[:apartment])

不确定为什么这会导致错误,直到很久以前它一直工作得很好,如果有人能帮助它,那就不胜感激了。



新观点:

<div id="new"><center><h1>New apartment</h1>
<%= render 'form' %>
<br />
<%= link_to 'Back', apartments_path %></center>
</div>



_形式查看:

<div id="formApt">
<%= form_for @apartment, :html => { :multipart => true } do |f| %>
  <% if @apartment.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@apartment.errors.count, "error") %> prohibited this apartment from being saved:</h2>
  <ul>
  <% @apartment.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
  <% end %>
    <div class="field">
    <%= f.label :agent %><br />
   <%= f.collection_select :agent, Apartment::AGENTS, :to_s, :to_s, 
   :include_blank => true %><br />
         </div>
  <div class="field">
    <%= f.label :location %><br />
    <%= f.text_field :location %><br />
  </div>
    <div class="field">
    <%= f.label :availability %><br />
     <%= f.collection_select :availability, Apartment::AVAILSEL, :to_s, :to_s, 
   :include_blank => true %><br />
  </div>
  <div class="field">
    <%= f.label :Bed %><br />
    <%= f.number_field :Bed %><br />
  </div>
  <div class="field">
    <%= f.label :Bath %><br />
    <%= f.number_field :Bath %><br />
   </div>
  <div class="field">
    <%= f.label :Rent %><br />
    <%= f.text_field :Rent %><br />
  </div>
  <div class="field">
    <%= f.label :Pets %><br />
     <%= f.collection_select :Pets, Apartment::PETSSEL, :to_s, :to_s, 
   :include_blank => true %><br />
  </div>
   <div class="field">
    <%= f.label :Security_Deposit %><br />
     <%= f.text_field :Security_Deposit %><br />
  </div>
   <div class="field">
    <%= f.label :Parking %><br />
    <%= f.text_field :Parking %><br />
  </div>
  <div class="field">
    <%= f.label :Laundry %><br />
      <%= f.collection_select :Laundy, Apartment::LAUNDRYSEL, :to_s, :to_s, 
   :include_blank => true %><br />
  </div>
   <div class="field">
    <%= f.label :Owner %><br />
    <%= f.text_field :Owner %><br />
  </div>
  <div class="field">
    <%= f.label :Date_Avail %><br />
    <%= f.date_select :Date_Avail %><br />
  </div>
  <div class="field">
    <%= f.label :Type_Heat %><br />
    <%= f.collection_select :Type_Heat, Apartment::HEATSEL, :to_s, :to_s, 
   :include_blank => true %><br />
  </div>
  <div class="field">
    <%= f.label :AC %><br />
     <%= f.collection_select :AC, Apartment::ACSEL, :to_s, :to_s, 
   :include_blank => true %><br />
  </div><br />  
    <div class="field">
    <%= f.file_field :photo %><br />
     <%= f.file_field :photo2 %><br />
     <%= f.file_field :photo3 %><br />
      <%= f.file_field :photo4 %><br />
  </div>
    <div class="field">
    <%= f.label :Description %><br />
    <%= text_area "apartment", "description", "cols" => 60, "rows" => 10 %><br />
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
    <div class="field">
  <% f.fields_for :apartment_image do |apartment| %>
  <% if apartment.object.new_record? %>
  <p>
  <%= apartment.label :caption, "Image Caption" %>
  <%= apartment.text_field :caption %>
  </p>
  <p>
 <% end %>
  </p> 
   </div>
   <% end %><% end %>
</div>



显示视图

<head>
<!-- First, add jQuery (and jQuery UI if using custom easing or animation -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  <%= javascript_include_tag "jquery.timers-1.2" %>
  <%= javascript_include_tag "jquery.easing.1.3" %>
  <%= javascript_include_tag "jquery.galleryview-3.0-dev" %>
 <%= stylesheet_link_tag "jquery.galleryview-3.0-dev", :media => "all" %>
<!-- Lastly, call the galleryView() function on your unordered list(s) -->
<script type="text/javascript">
    $(function(){
        $('#myGallery').galleryView({
    transition_speed: 2000,         //INT - duration of panel/frame transition (in milliseconds)
    transition_interval: 4000,      //INT - delay between panel/frame transitions (in milliseconds)
    easing: 'swing',                //STRING - easing method to use for animations (jQuery provides 'swing' or 'linear', more available with jQuery UI or Easing plugin)
    show_panels: true,              //BOOLEAN - flag to show or hide panel portion of gallery
    show_panel_nav: false,          //BOOLEAN - flag to show or hide panel navigation buttons
    enable_overlays: true,          //BOOLEAN - flag to show or hide panel overlays

    panel_width: 900,               //INT - width of gallery panel (in pixels)
    panel_height: 500,              //INT - height of gallery panel (in pixels)
    panel_animation: 'slide',       //STRING - animation method for panel transitions (crossfade,fade,slide,none)
    panel_scale: 'crop',            //STRING - cropping option for panel images (crop = scale image and fit to aspect ratio determined by panel_width and panel_height, fit = scale image and preserve original aspect ratio)
    overlay_position: 'bottom',     //STRING - position of panel overlay (bottom, top)
    pan_images: true,               //BOOLEAN - flag to allow user to grab/drag oversized images within gallery
    pan_style: 'drag',              //STRING - panning method (drag = user clicks and drags image to pan, track = image automatically pans based on mouse position
    pan_smoothness: 15,             //INT - determines smoothness of tracking pan animation (higher number = smoother)
    start_frame: 1,                 //INT - index of panel/frame to show first when gallery loads
    show_filmstrip: true,           //BOOLEAN - flag to show or hide filmstrip portion of gallery
    show_filmstrip_nav: true,       //BOOLEAN - flag indicating whether to display navigation buttons
    enable_slideshow: false,            //BOOLEAN - flag indicating whether to display slideshow play/pause button
    autoplay: false,                //BOOLEAN - flag to start slideshow on gallery load
    show_captions: true,            //BOOLEAN - flag to show or hide frame captions 
    filmstrip_size: 3,              //INT - number of frames to show in filmstrip-only gallery
    filmstrip_style: 'scroll',      //STRING - type of filmstrip to use (scroll = display one line of frames, scroll filmstrip if necessary, showall = display multiple rows of frames if necessary)
    filmstrip_position: 'bottom',   //STRING - position of filmstrip within gallery (bottom, top, left, right)
    frame_width: 164,               //INT - width of filmstrip frames (in pixels)
    frame_height: 80,               //INT - width of filmstrip frames (in pixels)
    frame_opacity: 0.5,             //FLOAT - transparency of non-active frames (1.0 = opaque, 0.0 = transparent)
    frame_scale: 'crop',            //STRING - cropping option for filmstrip images (same as above)
    frame_gap: 5,                   //INT - spacing between frames within filmstrip (in pixels)
    show_infobar: true,             //BOOLEAN - flag to show or hide infobar
    infobar_opacity: 1              //FLOAT - transparency for info bar
    });
});
</script>
<style type="text/css">
    body { 
        margin: 2em; 
        font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<div id="new"><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br />
    <ul id="myGallery">
        <li> <%= image_tag @apartment.photo.url (:large) %>
        <li> <%= image_tag @apartment.photo2.url (:large) %>
        <li> <%= image_tag @apartment.photo3.url (:large) %>
        <li> <%= image_tag @apartment.photo4.url (:large) %>
</ul>   
    <p>
  <b>Agent:</b>
   <%= @apartment.agent %>
</p>
 <p>
  <b>Location:</b>
  <%= @apartment.location %>
</p>
 <p>
  <b>Availability:</b>
  <%= @apartment.availability %>
</p>
 <p>
  <b>Bed:</b>
  <%= @apartment.Bed %>
</p>
<p>
  <b>Bath:</b>
  <%= @apartment.Bath %>
</p>
<p>
  <b>Rent:</b>
  $<%= @apartment.Rent %>
</p>
<p>
  <b>Pets:</b>
  <%= @apartment.Pets %>
</p>
<p>
  <b>Security deposit:</b>
  $<%= @apartment.Security_Deposit %>
</p>
<p>
  <b>Parking:</b>
  <%= @apartment.Parking %>
</p>
<p>
  <b>Laundy:</b>
  <%= @apartment.Laundy %>
 </p>
 <p>
  <b>Owner:</b>
  <%= @apartment.Owner %>
 </p>
  <p>
  <b>Date avail:</b>
  <%= @apartment.Date_Avail %>
 </p>
 <p>
  <b>Type heat:</b>
  <%= @apartment.Type_Heat %>
 </p>
<p>
  <b>Ac:</b>
  <%= @apartment.AC %>
 </p>
<p>
  <b>Description:</b>
  <%= @apartment.description %>
</p>

<center>
<nav2>

<span id="home2"> <%= link_to image_tag("home.png"), root_url %></span>
    <span id="apartments2"><%= link_to image_tag("apartments.png"), {:action =>'aptMenu', :controller => 'apartments'} %></span>
    <a href="http://livewellchicago.herokuapp.com/?cat=forms"> <%= image_tag("forms.png") %>            </a>
   <a href="http://livewellchicago.herokuapp.com/?cat=landlords">
    <%= image_tag("landlords.png")%>    </a>
    <a href="http://livewellchicago.herokuapp.com/?cat=contactUs">
    <%= image_tag("contactus.png") %>    </a>


</nav2>
</div>
</body>

PARAMS:

{"commit"=>"Create Apartment",
 "apartment"=>{"Laundy"=>"In Building",
 "availability"=>"RENTED",
 "Owner"=>"mike",
 "Date_Avail(1i)"=>"2012",
 "Date_Avail(2i)"=>"8",
 "agent"=>"Jason Gold",
 "Security_Deposit"=>"1200",
 "Date_Avail(3i)"=>"23",
 "Rent"=>"3232",
 "Bed"=>"3232",
 "Type_Heat"=>"Radiator",
 "location"=>"ewew",
 "Bath"=>"32323",
 "Pets"=>"Cats Only",
 "Parking"=>"no",
 "AC"=>"Window Unit",
 "photo"=>#<ActionDispatch::Http::UploadedFile:0x103e34c80 @content_type="image/jpeg",
 @tempfile=#<File:/var/folders/bg/dnbp4t6s12g5bh3_yrj6ntvr0000gn/T/RackMultipart20120823-62841-rogrlw-0>,
 @headers="Content-Disposition: form-data; name=\"apartment[photo]\"; filename=\"livingroom.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @original_filename="livingroom.jpg">,
 "description"=>"description goes here"},
 "authenticity_token"=>"3KhQ0Bh3D+DZSTbPPPhWmgJLqTQ7HyOx45Coa23N4mI=",
 "utf8"=>"✓"}

2 个答案:

答案 0 :(得分:1)

好吧,所以基本上这是我自己的错。与公寓模型和has_attached_file语句的格式有关

    has_attached_file :photo, :styles => {:small => "150x150>", :large => "800x720>", :convert_options => { :all =>     '-auto-orient' }}

essentually:convert_options部分导致图像的参数为nil,这阻止了任何东西被添加到数据库中(除非你避免添加图片)。但感谢普鲁斯万的帮助!

答案 1 :(得分:0)

<%= text_area "apartment" ... %>

应该是:

<%= f.text_area ... %>

编辑:这两个版本实际上基于this,因此问题出在其他地方。