-1

I am trying to submit a form in rails that is just a pdf uplaod (using paperclip). There is something wrong with either my form, controller or model and i am not sure which.

this is my form:

<%= form_for @yearguide, :html => { :multipart => true } do |form| %>
<%= form.file_field :pdf %>

<%= form.submit "Add Event", class: "btn btn-primary" %>
<% end %> 

my controller:

  class YearController < ApplicationController

    def new
    @yearguide = Year.create(year_params)
end

 def create
if @yearguide = Year.save
  redirect_to '/'
else
    render 'new'
end
 end

my model:

  class YearlyGuide < ActiveRecord::Base
has_attached_file :pdf
   validates_attachment :document, content_type: { content_type: "application/pdf" }
   end

my routes:

    resources :year

I add the file and press upload, but I am being redirected to 'update.html.erb'. The file doesn;t exist in the db, just an empty record.

When i debug the params on pressing uplaod I get this output

    {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"G7ZrXEiip/gsqhDObcfYhTT9kerYZGk+Zl29kWA5jos=", "year"=>{"pdf"=>#<ActionDispatch::Http::UploadedFile:0x000001029b0340 @tempfile=#<Tempfile:/var/folders/ns/ry6z7jfd6qg6j8xr2q6dw0yc0000gn/T/RackMultipart20140609-21455-1eg1sk3>, @original_filename="Artsmill Hebden Bridge Exhibition Programme 2014.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"year[pdf]\"; filename=\"Artsmill Hebden Bridge Exhibition Programme 2014.pdf\"\r\nContent-Type: application/pdf\r\n">}, "commit"=>"Add Event", "action"=>"update", "controller"=>"year", "id"=>"9"}

========================= EDIT

OK, so discrepancies with my naming led to the previosu errors, i started again, generating:

 rails g model YearlyGuide pdf:attachment start:datetime end:datetime

 rails g controller YearlyGuide new index show

now in my routes i have added

   resources :yearly_guides

when i visit

   /yearly_guides/new

I get this error

 uninitialized constant YearlyGuidesController

I am really at a loss as to what I am doing wrong, I have done this before and never had these issues.

@iceman, thanks for your help and patience thus far.

tereško
  • 56,151
  • 24
  • 92
  • 147
rico_mac
  • 810
  • 6
  • 21

2 Answers2

2

The controller is not doing what it's supposed to do. This is the bare bones basic scheme of creating a new object in Rails.

class YearsController < ApplicationController
  def new
    @yearguide = Year.new
  end

  def create
    @yearguide = Year.create(year_params)
    if @yearguide.save
      redirect_to '/' # I would consider redirect_to @yearguide to show the newly created object
    else
      render 'new'
    end
  end
end

EDIT:

You have to update your routes.rb to

resources :years
Eyeslandic
  • 12,640
  • 12
  • 34
  • 47
0

Since you are creating the yearguide object, rails infer that you have to do put/patch request, so request is going to update since rails getting id.

You have two options. 1) Change new method of controller like below

class YearController < ApplicationController

 def new
   @yearguide = Year.new
 end
end

2) Override the method parameter by passing method params as 'post' inside your form tag

Sanjiv
  • 653
  • 4
  • 12
  • changed to this, { :method=>"post" } do |form| %> I get error 'undefined method `years_path' for #:0x0000010c5c0bb8> " – rico_mac Jun 09 '14 at 12:22