I am trying to execute an AJAX routine with rails
the code runs normally but the response doesnt..
I get this error
Template is missing
Missing template line_items/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "D:/ruby/depot/app/views"
I created the create.js.rjs inside line_items directory like the book Agile Web Development says it is to be, but the error insists..
#file line_items_controller.rb
def create
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = add_product(#cart, product.id)
respond_to do |format|
if #line_item.save
format.html { redirect_to store_url }
format.js
format.json { render json: #line_item, status: :created, location: #line_item }
#...
end
end
end
and inside my create.js.rjs I got this
page.replace_html('cart', render(#cart))
Rjs is not a valid format anymore I guess. Use js.erb instead. Also manual you're following is outdated since none uses prototype anymore.
Related
i'm trying to update the user information when on mobile but rails log show:
someone know why this happen on mobile mode?
Missing template users/update, application/update with
{:locale=>[:"en"], :formats=>[:mobile], :handlers=>[:erb, :builder,
:coffee]}. Searched in:
def update
#user = User.find(params[:id])
respond_to do |format|
format.html do
#states = State.all
#cities = City.where('state_id = ?', State.first.id)
if #user.update_attributes(params[:user])
redirect_to(action: :edit, id: #user, only_path: true, format: :html)
flash[:notice] = 'updated'
else
render :edit
end
end
format.json do
#user.update_attributes(params[:user])
render nothing: true
end
end
end
You have format.html do and format.json do but you don't have a format.mobile do block.
Since the html and json blocks aren't executed, the code falls through and the default for any action is to render the format with the name of the action, so you're in the action update therefore rails by default looks for a view update.html.erb or update.html.haml
When I run my code on Cloud9 IDE, the next error appears:
Missing partial submissions/_submission with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates" * "/home/ubuntu/workspace/app/views" * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views"
I have a Submissions Controller and my create code is like this:
def create
#submission = Submission.new(submission_params)
respond_to do |format|
if #submission.save
format.html { redirect_to root_path}
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
I don't know which is my error and where to locate it.
Plus, a full stack text http://pastebin.com/YnyQeetU
The code of index.html.erb http://pastebin.com/fZbXd0Wk
This line of code is causing the problem.
<!--<%= render #submissions %>-->
#<!-- --> This is used to comment an HTML tag
If you do not want to render the partial you can simply comment that out as follows:
<%#= render #submissions %>
#<%# %> This is the way to comment embedded ruby code
Though the line is commented in HTML, the server tag is still being executed. Try to comment the server tag with <%#= link_to 'New Submission', new_submission_path %>. This will stop executing the server tag and you will not get the error.
More reference: How to comment lines in rails html.erb files?
I have the following ajax function
$.ajax({
url: '/sub_categories/sub_cat',
data: 'sub_cat=45',
success: function() {
alert('success');
}
})
Here is my controller
require 'json'
class SubCategoriesController < ApplicationController
def show
end
def sub_cat
#sub_categories = SubCategory.where(category_id: params[:cat_id])
html = render_to_string 'sub_categories/sub_cat'
response_html true,html
end
end
My application controller
def response_html status,html
respond_to do |format|
format.json {render json: {
status: status,
html: html,
}
}
format.html
end
end
I have json file in sub_categories/sub_cat.json.erb
When I run getting error as
ActionView::MissingTemplate at /sub_categories/sub_cat.json
Missing template sub_categories/show, application/show with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/editmehere/Documents/site/name/app/views"
My route.rb has
resources :sub_categories do
get 'sub_cat', on: :collection
end
Why I am getting error like this and how can I solve it. Can anyone help me to solve it.
I'm guessing you're trying to keep your application dry, but why don't you just use this in your SubCategoriesController:
class SubCategoriesController < ApplicationController
def sub_cat
#sub_categories = SubCategory.where(category_id: params[:cat_id])
respond_to do |format|
format.json
format.html
end
end
end
This will allow you to call sub_cat.json.erb without having to pass the status var, or pre-render the HTML. This is just convention, so apologies if it's not what you need. I see a lot of people on here overcomplicate things, when simplicity would work much better
Ajax
Also, I believe you've got a problem with your ajax data var:
data: 'sub_cat=45',
should be
data: {sub_cat: "45"},
In my routes.rb I have:
resources :workouts
In my workouts controller I have:
def show
respond_to do |format|
format.html
format.json { render :json => "Success" }
end
end
But when I go to /workouts/1.json, I receive the following:
Template is missing
Missing template workouts/show, application/show with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/rails/app/views"
Which appears to show that the format is what it should be but it's still searching for a view. This same code functions in other controllers with identical setups just fine. Also, going to /workouts/1 for the html view seems to work just fine, though it also renders the html view properly when the format.html is removed.
Looks at the source code of render
elsif options.include?(:json)
json = options[:json]
json = ActiveSupport::JSON.encode(json) unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type ||= Mime::JSON
render_for_text(json, options[:status])
Pay attention to the third line. If the value of :json is a string, render won't call to_json automatically for this value.
So the value remains as string and render will go on to search template.
To fix, supply a valid hash even for trying purpose.
format.json { render :json => {:message => "Success"} }
You would try with /workouts.json
I'm trying to render a liquid template that is stored in the database.
Here is my 'show' action in the controller:
def show
#organization = Organization.find_by_subdomain(request.subdomain)
#template = Liquid::Template.parse(Template.find(#organization.current_template).body)
#page = #organization.pages.find(params[:id])
respond_to do |format|
format.html { render #template.render('page' => #page), :template => false}
format.json { render json: #page }
end
end
However, when I visit the page, I get a "Template is Missing" exception with the following error (note that "testing testing" is the body attribute of the page object, which is currently the only thing being rendered in the template):
Missing template /testing testing with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}.
Searched in: * "/Users/ashercohen/Documents/Rails/Vocalem-Rails/app/views"
* "/Users/ashercohen/.rvm/gems/ruby-1.9.3-p194/gems/twitter-bootstrap-rails-2.1.1/app/views"
* "/Users/ashercohen/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/app/views"
Why is it trying to find another template, when I've specifically pass the :template => false argument? Is there something I'm missing here? I'm trying to bypass using any template files, as it seems like they shouldn't be needed here (though am not strongly opposed if I am wrong).
Because render almost always takes a filename, while #template.render('page' => #page) contains the plain html. You should invoke render like this:
render :text => #template.render('page' => #page), :content_type => :html