title: Rails: Having a button in a view call a method in a controller, route not working
body: <p>Hi I'm making a rails app that uses Zendesk API calls. I have a controller that uses two classes I defined </p>
  
  <pre><code>class TicketsController &lt; ApplicationController
  
    require 'ticket_fields'
    require 'ticket_search'
  
    def getTickets
      @search_fields = SearchFields.new(client)
      @tickets = TicketSearch.new(client)
    end
  
    def search_tickets
      @ddcustomvalues = [params[:customer_company_id], params[:study_id], params[:type_id], params[:system_id]]
      @tickets.search_tickets(ddcustomvalues)
    end
  end
  </code></pre>
  
  <p>One class SearchFields uses the api to load values I want to filter tickets by into arrays. My view then uses these values to populate drop down lists.</p>
  
  <p>The other class TicketSearch looks like this. </p>
  
  <pre><code>class TicketSearch
  
    attr_reader :tickets, :text
  
    def initialize(client)
      @text = "query"
      @tickets = Array.new
      client.tickets.all do |resource|
        @tickets &lt;&lt; resource
      end
    end
  
  
    def search_tickets(custom_search_fields)
      querystring = "type:ticket+tags:"
      custom_search_fields.each_with_index do |field, index|
        unless field == ""
          if index ==0
            querystring += "#{field}"
          else
            querystring += " #{field}"
          end
        end
      end
  
      @text = querystring
  
    end
  
  end
  </code></pre>
  
  <p>What I want to happen in my view is when a button is pressed it changes the value of @text to the querystring generated by the drop down list options that were selected. I'm currently doing this for testing to see if my querystring is correct and the button works. What I eventually want it to do is send the querystring to the ZenDesk Server and returns the tickets I filtered for. the @tickets array would then be replaced with the filtered tickets the server returned. Currently my button code looks like this.</p>
  
  <pre><code>&lt;%= button_to 'Search', :action =&gt; 'search_tickets' %&gt;
  </code></pre>
  
  <p>with all the route code I've tried I either get an error upon starting the page. Or when I press the button nothing happens and the @text being displayed in my view remains "query". Can someone help explain what I need to do I don't quite understand how routes work.</p>
  
  <p>==================================================================================</p>
  
  <p>Hey so I made the changes you suggested and did some reading up on AJAX and js and I think I'm almost at the answer my view now looks like this</p>
  
  <pre><code>&lt;div id="test" &gt;
  &lt;%= render partial: 'text', locals: { text: @tickets.text} %&gt;
  &lt;div id="test" &gt;  
  </code></pre>
  
  <p>and I created a partial _text file that looks like this  </p>
  
  <pre><code>&lt;p&gt; Query: &lt;%=text%&gt; &lt;/p&gt; 
  </code></pre>
  
  <p>and a js file search_tickets.js.erb  </p>
  
  <pre><code>$("#test").html("&lt;%= escape_javascript(render partial: 'text', locals: { text: @tickets.text } ) %&gt;");  
  </code></pre>
  
  <p>any idea what may be going wrong everything loads up okay but the text remains the same in the partial i set up when i hit the button still</p>
  
  <p>the console outputs this after the button is hit</p>
  
  <p>ActionController::RoutingError (No route matches [POST] "/tickets/search_tickets"):</p>
  
  <p>so I guess it may actually be a routing error my route looks like this </p>
  
  <pre><code>resources :tickets do
      collection do
        put :search_tickets
      end
    end
  </code></pre>
  
  <p>and the form tag calling the path looks like this </p>
  
  <pre><code>  &lt;%= form_tag search_tickets_tickets_path, remote: :true do %&gt;
        &lt;table&gt;
            &lt;tr&gt;
              &lt;td align = "left" valign="middle"&gt; &lt;font size = 4&gt; Customer Company &lt;/font&gt; &lt;/td&gt;
              &lt;td align = "left" valign="middle"&gt;
                &lt;%= select_tag "customer_company_id", options_for_select(@search_fields.customer_companies), :prompt =&gt; "Select One" %&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
            ......
            &lt;tr&gt;
               &lt;td&gt; &lt;/td&gt;
               &lt;td align = "left" valign="middle"&gt;
                &lt;%= submit_tag "Search" %&gt;
               &lt;/td&gt;
            &lt;/tr&gt;
         &lt;/table&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>==================================================================================
  (Update)</p>
  
  <p>I think I fixed my last problem by changing my form tag to this </p>
  
  <pre><code>  &lt;%= form_tag search_tickets_tickets_path(@tickets), method: :put, remote: :true do%&gt;
  </code></pre>
  
  <p>however now I get this error from the terminal after I hit the button</p>
  
  <p>NoMethodError (undefined method <code>search_ticket' for nil:NilClass):
    app/controllers/tickets_controller.rb:15:in</code>search_tickets'</p>
  
  <p>how would I pass @tickets as a parameter through my route because clearly its not accessible by search_tickets right now as its giving a nil class error.</p>