Tag Archives: partials

Missing Partial (JSON Response) – Ruby on Rails

Hey everyone,

I’ve just added pagination (Kaminari) to make an infinite scroll list a little easier to use. Unfortunately, I ran into the following error – had me stumped for a while:

Missing partial /contents/content_list_item with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in:  * "/home/chris/funny/app/views"  * "/home/chris/.rvm/gems/ruby-1.9.3-p327/gems/kaminari-0.14.1/app/views"

The solution was fairly simple, but adding a few things at once had me looking at it the wrong way. Because I was using the same nested partials for an HTML response as my JSON one, the file extensions weren’t what rails was expecting. To get around this, all I needed to do was fully qualify the partial filenames. For instance:

1
2
3
4
5
#The original
<%= render :partial => '/contents/content_list_item', :locals => { :@content_item =>  content} %>
 
#Became this
<%= render :partial => '/contents/content_list_item.html.erb', :locals => { :@content_item => content} %>

Will_Paginate with Sunspot – Ruby on Rails

Hey all,

I’ve just gone through setting up will_paginate with sunspot 1.3, just thought I’d share in case anyone else has any troubles/improvements.

In my case I’m setting it up on a products model.

  #products.rb
 
  #Sunspot/Search
  searchable do
    text :title, :boost => 5
    text :description
  end

#products_controller.rb
  #Products search
  def search
 
    #Create vars
    search = Product.search do
      fulltext params[:search]
      paginate :page => params[:page] || 1, :per_page => 2
    end    
 
    #Retrieve results
    @products = search.results
 
    #Create responses
    respond_to do |format|
      format.html { render 'index' }
    end
  end

#Search partial - _search.html.erb
<%= form_tag :controller => 'products', :action => 'search', :method => :get do %>
   <%= text_field_tag :search, params[:search], :size => '25', :class => 'search' %>
<% end %>

#Product display page - index.html.erb
<% @products.each do |product| %>
   <%= render :partial => "products/product_line", :locals => {:product => product} %>
<% end %>
 
<%= will_paginate @products, :container => false, :params => { :search_text => params[:search_text] } %>

Well that’s about it, if anyone needs any further info or has any tips, leave a comment and I’ll get back to you asap.

Passing Variables to a Partial from a View – Ruby on Rails

Just another one of those things that I find myself needing to do from time to time. Luckily this is all pretty straight forward (thankyou Ruby on Rails!). Assuming your partial is a form in the locations folder and that the variable you’re expecting is location all that you have to do is following:

View: users/show.html.erb

1
<%= render :partial => 'locations/form', :locals => { :location => @user.location } %>

Partial: locations/_form.html.erb

1
2
 
Street: <%= location.street %>

Good luck!