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.
Related
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.
I have this two methods on catalog_controler.rb
def latest
#boxes = Box.latest 5
#page_title = 'Novedades'
end
def rss
latest
render :layout => false
end
end
on app/views/catalog folder I have this xml file , rss.xml.erb
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title #page_title
xml.link(url_for(:action => "index", :only_path => false))
xml.language "en-us"
xml.ttl "40"
xml.description "International Boxes"
for book in #books
xml.item do
xml.title(book.title)
xml.description("#{box.model})
xml.pubDate(book.created_at.to_s(:long))
xml.guid(url_for(:action => "show", :id => book, :only_path => false))
xml.link(url_for(:action => "show", :id => book, :only_path => false))
end
end
end
end
But it gives an missing template error:
Missing template catalog/rss, application/rss with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/user/desktop/eshop_con_CSS/app/views"
How can I show the xml file on browser?
Read about MimeResponds's respond_to method. It should look like below, please change the block content as per your requirement.
def rss
#latest = latest
respond_to do |format|
format.xml { render xml: #latest }
end
end
respond_to can adopt to various different MIMES including popular .json, which is very handy in building/working-with APIs.
I am attempting to write a test verifying my controller's action. When tested manually, the code executes as expected. However, my test raises an error because it is unable to locate a template I am attempting to render.
Snippet from my controller code:
response = {:success => true}
response[:html] = {}
response[:html][:list] = render_to_string :partial => "data_source", :locals => {:data_source => #data_source}
respond_to do |format|
format.json {render :json => response}
end
My test:
before do
#data_source = FactoryGirl.create :data_source, :facebook_fan_page, :account_id => #account.id, :user_id => #user.id
post :delete, :format => :json, :id => #data_source.id
end
it "should disable the data source" do
assert_equal false, #data_source.reload.enabled?
end
The error message:
ActionView::MissingTemplate: Missing partial data_sources/data_source,
capture/data_source, application/data_source with {:locale=>[:en],
:formats=>[:json], :handlers=>[:erb, :builder, :haml]}. Searched in:
* "/Users/me/code/my_app/app/views" * "/Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/konacha-3.0.0/app/views"
from
/Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.17/lib/action_view/path_set.rb:58:in
`find'
There is definitely a partial in the app/view/data_sources directory "_data_source.html.haml".
Why can't the test find the partial? I suspect a setup issue
Thanks in advance.
As you are doing a json request, render_to_string is looking for a json template. Try this:
render_to_string :partial => "data_source", :formats => [:html], :locals => {:data_source => #data_source}
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"))%>")
I have the following, rather trivial, code that should log a user into my application using Devise. This actually works just fine if it's done using an HTML request rather than XHR. When I do it this way, the XHR is made,
= form_for(#user, :url => session_path(#user), :remote => true) do |f|
= f.label :email
= f.text_field :email, :size => 15, :maxlength => 32
= f.label :password
= f.password_field :password, :size => 15, :maxlength => 32
%br
= f.submit "Sign in"
and the result is:
Started GET "/" for 127.0.0.1 at 2012-12-04 13:24:44 -0800
Processing by HomeController#index as HTML
Rendered home/_hello_page.html.haml (0.1ms)
Rendered home/index.html.haml within layouts/application (1.9ms)
Completed 200 OK in 18ms (Views: 16.3ms | ActiveRecord: 0.4ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2012-12-04 13:24:54 -0800
Processing by Devise::SessionsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1cGaycA20NMhK0fOwQyN8e3aSFwCHB6BZcLwmvKTI3U=", "user"=>{"email"=>"obscured#someplace.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
Completed 500 Internal Server Error in 65ms
ActionView::MissingTemplate (Missing template devise/sessions/create, devise/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
* "/Users/sxross/Developer/iPhoneApps/motion/whos_here/whos_here_server/app/views"
* "/Users/sxross/.rvm/gems/ruby-1.9.3-p327#whos_here_server/gems/devise-2.1.2/app/views"
):
Everything is exactly as I expect it and it's clear that Processing by Devise::SessionsController#create as JS is telling the receiving controller "ship me back the JSON I want." So the question is why is the Devise controller trying to render a template instead of JSON and what can I do to fix it?
Thanks!
You are missing a create.js.[erb|haml] file in views/devise/sessions
When you send a remote request for login, the request is done by Devise::SessionsController#create, and if you don't override it
respond_with resource, :location => after_sign_in_path_for(resource)
will look for a create.js.erb.
I ussualy just have
window.location = "<%= root_url %>"
in it.
Also, you would need to set
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:"*/*", "*/*", :html, :js]
inside config/initializers/devise.rb for this to work.
I for my own case have overridden the sessions controller with this:
#resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
sign_in(resource_name, #resource)
respond_to do |format|
format.html { respond_with #resource, :location => after_sign_in_path_for(#resource) }
format.js
end
which throws me to a failure method if the authentication has failed, and process it in failure.js.erb to show the user that