title: Rails controller update method only works for one call, then fails?
body: <p>I have a simple url that will toggle one field in my search model.  (locked) I want to toggle this field by clicking on a link.</p>
  
  <p>example:</p>
  
  <pre><code>http://localhost:3000/search/toggle/fe5c72164908af20a7727f324e2fdbc1
  </code></pre>
  
  <p>The link works fine for one call then gives me the following error:</p>
  
  <pre><code>undefined method `locked' for nil:NilClass
  </code></pre>
  
  <p>the toggle path is just pointed to the searches update method:</p>
  
  <p>route:</p>
  
  <pre><code>map.connect 'search/toggle/:id', :controller =&gt; 'searches', :action =&gt; 'update'
  </code></pre>
  
  <p>update action looks like: </p>
  
  <pre><code>  def update
  
     @search = Search.find_by_permalink(params[:id])
  
     if @search.locked == 1 then 
       @search.locked = 0 
     else 
       @search.locked = 1
      end 
  
      respond_to do |format|
        if @search.update_attributes(params[:search])
          flash[:notice] = 'Search was successfully updated.'
          format.xml  { head :ok }
          format.js
        else
          format.html { redirect_to('/users/current') }
          format.js
        end
      end
    end
  </code></pre>
  
  <p>I also have a ajax call looks like: </p>
  
  <pre><code>$j('a.lock-status').live('click', function(e) {
      $j.get($j(this).attr('href'));
      e.preventDefault();
  });
  </code></pre>
  
  <p>To summarize:</p>
  
  <p>If I enter a toggle URL into the URL bar of the browser it works once.  If I hit refresh it fails and gives me the above error.</p>
  
  <p>If I use the ajax call it works once.</p>
  
  <p>If I use the ajax call then refresh the page, it will work for that first request every time.</p>
  
  <p>I have a feeling this might have something to do with GET/POST requests in rails but I'm not sure?  I have narrowed it down to:</p>
  
  <pre><code>@search = Search.find_by_permalink(params[:id])
  </code></pre>
  
  <p>retuning nill on any call after the first one, but I'm not sure why its doing that?</p>
  
  <p><strong>UPDATE 1:</strong>  </p>
  
  <p>Here is the log output for 2 requests. (1st works, 2nd fails):</p>
  
  <pre><code>Processing SearchesController#update (for 127.0.0.1 at 2011-06-13 16:19:39) [GET]
    Parameters: {"action"=&gt;"update", "id"=&gt;"9036304997196d83b20cba82a0cc67b8", "controller"=&gt;"searches"}
    Search Columns (1.0ms)   SHOW FIELDS FROM `searches`
    Search Load (0.4ms)   SELECT * FROM `searches` WHERE (`searches`.`permalink` = '9036304997196d83b20cba82a0cc67b8') LIMIT 1
    SQL (0.1ms)   BEGIN
    Search Update (0.2ms)   UPDATE `searches` SET `locked` = 1, `updated_at` = '2011-06-13 23:19:39', `permalink` = '97894f26ba36761d04575260b132c230' WHERE `id` = 282
    SQL (0.3ms)   COMMIT
  Rendering searches/update
  Completed in 17ms (View: 6, DB: 2) | 200 OK [http://localhost/search/toggle/9036304997196d83b20cba82a0cc67b8]
    SQL (0.1ms)   SET NAMES 'utf8'
    SQL (0.1ms)   SET SQL_AUTO_IS_NULL=0
  
  
  Processing SearchesController#update (for 127.0.0.1 at 2011-06-13 16:19:41) [GET]
    Parameters: {"action"=&gt;"update", "id"=&gt;"9036304997196d83b20cba82a0cc67b8", "controller"=&gt;"searches"}
    Search Columns (1.0ms)   SHOW FIELDS FROM `searches`
    Search Load (0.4ms)   SELECT * FROM `searches` WHERE (`searches`.`permalink` = '9036304997196d83b20cba82a0cc67b8') LIMIT 1
  
  NoMethodError (undefined method `locked' for nil:NilClass):
    app/controllers/searches_controller.rb:84:in `update'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:162:in `start'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:92:in `each'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /Users/Nick/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:82:in `start'
  
  Rendered rescues/_trace (109.8ms)
  Rendered rescues/_request_and_response (1.1ms)
  Rendering rescues/layout (internal_server_error)
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>OK, I see what you guys are saying... not sure what would be changing the permalink. I'm going to double check the model code in the AM.</p>
  
  <p>Thanks!</p>