Skip to content

Instantly share code, notes, and snippets.

@shruti28
Last active December 20, 2015 15:38
Show Gist options
  • Save shruti28/6155116 to your computer and use it in GitHub Desktop.
Save shruti28/6155116 to your computer and use it in GitHub Desktop.
<h1>Editing coupon</h1>
<%= form_for(@coupon, :html => {:multipart => true}) do |f| %>
<% if @coupon.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@coupon.errors.count, "error") %> prohibited this coupon from being saved:</h2>
<ul>
<% @coupon.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :data %>
<%= f.file_field :data %>
</div>
<div class="field">
<%= f.label :datalogo %>
<%= f.file_field :datalogo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Show', @coupon %> |
<%= link_to 'Back', coupons_path %>
>>>>>>>>>>>>>
controller:
def update
@coupon = Coupon.find(params[:id])
@coupon.mime_type=params[:coupon][:data].content_type
@coupon.filename = params[:coupon][:data].original_filename
@coupon.data= params[:coupon][:data].read
@coupon.mime_typelogo=params[:coupon][:datalogo].content_type
@coupon.filenamelogo = params[:coupon][:datalogo].original_filename
@coupon.datalogo= params[:coupon][:datalogo].read
respond_to do |format|
if @coupon.update_attributes(params[:coupon])
format.html { redirect_to @coupon, notice: 'Coupon was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @coupon.errors, status: :unprocessable_entity }
end
end
end
>>>>>>>>>>>>>>>>>>>>>>>>>>
Error:
NoMethodError in CouponsController#update
undefined method `name' for nil:NilClass
Rails.root: C:/Users/shruti.k/Desktop/ror/couponsys v2
Application Trace | Framework Trace | Full Trace
app/controllers/coupons_controller.rb:79:in `block in update'
app/controllers/coupons_controller.rb:78:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"g4T5is35CjQ2h8U1CDFClVIQYfBevNUMblytIvEwIOY=",
"coupon"=>{"name"=>"Shopper Stop",
"price"=>"20.0",
"description"=>"5% discount on a bill of more than 1000/-",
"data"=>#<ActionDispatch::Http::UploadedFile:0x299d168 @original_filename="coupon7.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"coupon[data]\"; filename=\"coupon7.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/shruti.k/AppData/Local/Temp/RackMultipart20130805-3780-12leu9m>>,
"datalogo"=>#<ActionDispatch::Http::UploadedFile:0x299cf10 @original_filename="logo1.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"coupon[datalogo]\"; filename=\"logo1.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/shruti.k/AppData/Local/Temp/RackMultipart20130805-3780-n4qv0r>>},
"commit"=>"Update Coupon",
"id"=>"9"}
@hrcl
Copy link

hrcl commented Aug 5, 2013

try this as the update_attributes:

  if @coupon.update_attributes(:mime_type => params[:coupon][:data].content_type,
                                              :filename  => params[:coupon][:data].original_filename,
                                              :data => params[:coupon][:data].read,
                                              :mime_typelogo => params[:coupon][:datalogo].content_type,
                                              :filenamelogo  => params[:coupon][:datalogo].original_filename,
                                              :datalogo => params[:coupon][:datalogo].read) 

@shruti28
Copy link
Author

shruti28 commented Aug 5, 2013

Its working perfectly now.:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment