title: Adding paperclip URL to JSON in Rails
body: <p>Please reference this question: <a href="https://stackoverflow.com/questions/5588185/how-can-i-get-url-for-paperclip-image-in-to-json">same question</a> or <a href="https://stackoverflow.com/questions/9650959/add-paperclip-url-to-json">another similar question</a></p>
  
  <p>I cannot seem to get my <code>work</code> scaffold to include the image URL from paperclip to JSON.  I need this url to do some ajax manipulation to the dom.</p>
  
  <p>To my understanding you add a def in a model and then you add a extra line to the format.json in the controller.  </p>
  
  <p>when I add the @work.avatar_url to my controller method <code>def create</code> it throws up a syntax error</p>
  
  <pre><code>syntax error, unexpected ',', expecting tASSOC
  </code></pre>
  
  <p>I'm really new to rails and MVC.  All the answers make it sound so easy.  Unfortunately, I'm just guessing and checking...</p>
  
  <h2>Controller Link: <strong><a href="https://github.com/matthewharwood/blog/blob/master/app/controllers/works_controller.rb" rel="nofollow noreferrer">works_controller.rb</a></strong></h2>
  
  <p>My Def Create</p>
  
  <pre><code>def create
      @work = Work.new(params[:work])
  
  
      respond_to do |format|
        if @work.save
          format.html { redirect_to @work, notice: 'Work was successfully created.' }
          format.json { render json: @work, status: :created, location: @work }
        else
          format.html { render action: "new" }
          format.json { render json: @work.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <h2>Git Link Model: <strong><a href="https://github.com/matthewharwood/blog/blob/master/app/models/work.rb" rel="nofollow noreferrer">work.rb</a></strong></h2>
  
  <pre><code>class Work &lt; ActiveRecord::Base
    validates :name, :presence =&gt; true
  
    has_many  :categoryworks
    has_many :categories, :through =&gt; :categoryworks
    accepts_nested_attributes_for :categories
    attr_accessible :name, :subtitle, :category_ids, :svg, :post_a, :post_b, :post_c, :post_d, :avatar
    has_attached_file :avatar, :styles =&gt; { :medium =&gt; "1280x700&gt;", :thumb =&gt; "100x100&gt;" }, :default_url =&gt; "/images/:style/missing.png"
  
     def avatar_url
       avatar.url(:medium)
     end
  
  end
  </code></pre>
  
  <hr>