Ruby on Rails: Will paginate AJAX not rendering the next page - ruby-on-rails

I am using the Will paginate gem with Ruby on Rails (4.2) and to render the pages I am using Ajax. I am using the will paginate helper to do it. And it is making the request for the pages correctly and receiving the response as well but not rendering the next HTML page.
Can anyone tell me what is missing in it?
Am using :
<%= ajax_will_paginate #itmes, :params => { :my_excluded_param => nil } %>
to make pagination links.
Here is my pagination helper:
module WillPaginateHelper
class WillPaginateAjaxLinkRenderer < WillPaginate::ActionView::LinkRenderer
def prepare(collection, options, template)
options[:params] ||= {}
options[:params]["_"] = nil
super(collection, options, template)
end
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
ajax_call = "$.ajax({url: '#{target}', dataType: 'script'});"
#template.link_to_function(text.to_s.html_safe, ajax_call, attributes)
end
end
def ajax_will_paginate(collection, options = {})
will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateAjaxLinkRenderer))
end
end
And my controller function is:
def index
#items = Item.paginate(:per_page => 5, :page => params[:page])
end
I added the code index.js.erb
and now the terminal log is:
and now the terminal log is:
Started GET "/items?page=2&_=1399700653418" for 127.0.0.1 at 2014-05-10 11:14:16 +0530
Processing by ItemsController#index as JS
Parameters: {"page"=>"2", "_"=>"1399700653418"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
Rendered items/index.js.erb (12.7ms)
Completed 500 Internal Server Error in 20ms
ActionView::Template::Error (Missing partial items/items, application/items with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/var/www/selectcom/app/views"
* "/home/randhir/.rvm/gems/ruby-2.1.0/gems/kaminari-0.15.1/app/views"
* "/home/randhir/.rvm/gems/ruby-2.1.0/gems/devise-3.2.4/app/views"
):
1: <%= "$('body').html('#{escape_javascript(render 'items')}');".html_safe %>
app/views/items/index.js.erb:1:in `_app_views_items_index_js_erb__4572043876272285855_69867408217600'

you did not create a partial file named items to be rendered on ajax request. Try as below.
app/controllers/items_controller.rb
def index
#items = Item.paginate(:per_page => 5, :page => params[:page])
end
views/items/index.html.erb
<div class = "sort_paginate_ajax"><%= render 'items' %></div>
views/items/_items.html.erb
<% #items.each do |item| %>
# your code
<%= item.name %>
<% end %>
<%= ajax_will_paginate #items %>
views/items/index.js.erb
$('.sort_paginate_ajax').html("<%= escape_javascript(render("items"))%>")

Related

link_to remote: true redirecting to HTML

At the /tags page I have a link with remote: true. It should be a link to an ajax request. But there are two requests, as JS and as HTML.
<%= link_to 'New', new_tag_path, class: "btn btn-outline btn-primary", remote: true %>
INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as JS
INFO -- : Rendered tags/_form.html.erb (41.0ms)
INFO -- : Rendered tags/_modal.html.erb (41.5ms)
INFO -- : Rendered tags/new.js.erb (49.2ms)
INFO -- : Completed 200 OK in 63ms (Views: 50.3ms | ActiveRecord: 2.2ms)
INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as HTML
INFO -- : Completed 500 Internal Server Error in 14ms (ActiveRecord: 1.9ms)
FATAL -- :
ActionView::MissingTemplate (Missing template tags/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
If I provide a new.html.erb, this MissingTemplate error is over, but the page is redirected to new.html.
What could be wrong with that request or that link?
Edit The controller code
class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy, :fix_correct_name]
before_action :set_tags, only: [:new, :edit]
# GET /tags/new
def new
#tag = Tag.new
end
# GET /tags/1/edit
def edit
end
# POST /tags
def create
#tag = Tag.new(tag_params)
respond_to do |format|
if #tag.save
format.html { redirect_to action: "index", notice: 'Tag adicionada.' }
else
format.html { render :new }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tag
#tag = Tag.find(params[:id])
end
def set_tags
#tags = Tag.all.pluck(:name, :id)
end
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params.fetch(:tag, {}).permit(:name, :parent_id, :tag_name, :parent_name, :no_parent)
end
end
In your new action, you’ll need a response_to to handle the ajax call
def new
#tag = Tag.new
respond_to { |format| format.js }
end
Also, you’ll need a new.js.erb file and a _new.html.erb partial to handle the response and update the view.
Inside your new.js.erb, you will have to render the view with something like this
$(“div-id-name”).html(“<%= j render partial: “new” %>”)
And your new partial will simply hold your form (or whatever is it you wanna render)
I made it work now, only replacing require jquery_ujs by require rails-ujs to application.js and adding rails-ujs gem.

Rails Template is missing render json

actually i use rails for my REST API, and i need transform my object to json but when i try i got this error:
<h1>Template is missing</h1>
<p>Missing template firms/show, application/show with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :arb, :jbuilder]}. Searched in:
* "/Users/allan/Desktop/Work/back/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/activeadmin-0.6.0/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/kaminari-0.16.3/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise_invitable-1.5.5/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise-3.5.4/app/views"
</p>
This is my code
def show
firm= Firm.find_by_subdomain(params[:subdomain])
if firm.present?
respond_to do |format|
#firm = firm
format.json { render :json => #firm.to_json }
end
end
end
I hope someone here can help me :)
Solve:
def show
render json: Firm.find_by_subdomain(current_subdomain)
end
thank you
Template missing means that you asking for a html view, not doing a json request.
If you want to always return json format regardless of format param, do this:
before_action :set_default_response_format
protected
def set_default_response_format
request.format = :json
end
#source: Rails 4 - How to render JSON regardless of requested format?
Try to add .json at the end of you query when querying your route. Without this, it will try to render html and it will go search for view file that might not be present in your case.

getting ActionView::MissingTemplate

i have a method called def createCustomer so i created a .html.erb file called createCustomer within the folder name charge which is the name of the controller. This charge folder is in under the views folder.
i am getting this on my logs
ActionView::MissingTemplate (Missing template charge/createCustomer, application/createCustomer with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: 2016-01-26T23:00:34.324498+00:00 app[web.1]: * "/app/app/views"
To add to the comments, you need to equate yourself with the Rails infrastructure (convention over configuration):
Class names are CamelCase, file names are snake_case
Controllers are plural (mostly)
You should keep your controllers resourceful
Here's how it should be structured:
# config/routes.rb
resources :customers #-> url.com/customers/new
# app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def new
#customer = Customer.new
end
def create
#customer = Customer.new customer_params
redirect_to #customer if #customer.save
end
private
def customer_params
params.require(:customer).permit(...)
end
end
# app/views/customers/new.html.erb
<%= form_for #customer do |f| %>
...

issue on render :update do

My session controller
class SessionsController < ApplicationController
skip_before_filter :is_loggedin?, :only => [:new, :create]
def new
render :action => "new", :layout => false
end
def create
render :update do |page|
if !params[:email].blank? && !params[:password].blank?
if user = User.authenticate(params[:email], params[:password])
if user == "unuser"
page.alert("Your username and/or password was incorrect.")
elsif !user.account_id.blank?
if params[:remember_me]
cookies.permanent[:user_id] = user.id
else
cookies[:user_id]=user.id
end
user.update_attributes(:last_login => Time.now, :is_online => true)
if user.user_type=="mentor"
page.redirect_to home_mentors_path(:account_id =>user.account_id)
else
page.redirect_to home_students_path(:account_id =>user.account_id)
end
else
page.alert("You have not confirmed the activation link yet. Please check your registered email ")
end
else
page << "jQuery('#login_error').html('Invalid user/password');"
end
else
page.alert("You can't leave this empty.")
end
end
My sessions/_form.htm is like this:
<%= form_tag sessions_path, :remote => true, :method =>"post" do %>
<div id="login-modal" class="shadow" style="display: block; z-index: 4000;">
<div class="login_bar_top">
<div class="login_bar_left"></div>
<div class="login_bar_mid">User Login</div>
<div class="login_bar_right"></div>
</div>
I have lot of controllers with code render :update do, now it will take lot of time to review all, with respond_to do. I tried by installing prototype-rails gem also. Not solved.
Error I am facing is:
Started POST "/sessions" for 127.0.0.1 at 2013-12-10 13:59:50 +0530
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"42cE1Mx5gXzmZAbSwgSnxbbygYLH4x81lbPjkCDKAOs=", "email"=>"mentor#gmail.com", "password"=>"[FILTERED]"}
Completed 500 Internal Server Error in 2ms
ActionView::MissingTemplate (Missing template sessions/update, application/update with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :rjs]}. Searched in:
* "/home/prz/project/techzoorails/techzoo3/app/views"
):
app/controllers/sessions_controller.rb:9:in `create'
Previous it was working fine. Is there any solution by keeping render :update code. I am using rails 3.0.9 with ruby 1.9.3p392.

Missing template action error in rails3

I have defined a method in controller that is like:
def self.dailymail
.... #fill data from db
ac = ActionController::Base.new()
kit = PDFKit.new(ac.render_to_string(:action => "formatinhtml.html.erb",:rawdata => data))
pdf = kit.to_pdf
... #send pdf in mail
end
formatinhtml is like:
def formatinhtml
#dailyrep = params[:rawdata]
respond_to do |format|
format.html # daily.html.erb
end
end
I have to use self.dailymail so that I can call it from model & in turn from rufus scheduler.But,still I get error such as:
scheduler caught exception:
Missing template action_controller/base/daily.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xls], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "F:/DEVELOPMENT/TrackIt/app/views"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/path_set.rb:58:in `find'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/lookup_context.rb:109:in `find'...
so,what does I need to do?
Update: After debugging,I found action formatinhtml is not actually being getting called;I have defined neccessary routes.
Your folder where "daily.html" is located is not the same as your controller

Resources