in my action mailer(rails 3.2.13) I have something like
class RepaymentMailer < ActionMailer::Base
default from: "repayments#milaap.org"
def repayment_mail user, user_repayment_info, month_date
#mail_layout = if condition_true
'layout1'
else
'layout2'
end
mail(to: "#{user.first_name} <#{user.email}>",
from: "xxx#xxx.org",
bcc: ["xxx <xxx#xxx.org>"],
reply_to: 'xxx#xxx.org',
subject: "this is a test" ) do |format|
format.html { render layout: #mail_layout }
format.text
end
end
end
when I do
puts RepaymentMailer.repayment_mail(param1, param2, param3).deliver
it gives me error that even if repayment_mail template exist
ActionView::MissingTemplate: Missing template repayment_mailer/repayment_mail with {:locale=>[:en], :formats=>[:text], :handlers=>[:erb, :builder, :coffee, :arb]}. Searched in:
If I remove
do |format|
format.html { render layout: #mail_layout }
format.text
end
then It works without error.
But Obviously I want the desired layout. What can be the cause of the error.
Because in your error, it is clearly showing that problem is with this line:
Missing template repayment_mailer/repayment_mail with {:locale=>[:en], :formats=>[:text]**, :handlers=>[:erb, :builder, :coffee, :arb]}.
I think you didn't read the lines below:
class UserMailer < ActionMailer::Base
def welcome_email(user)
mail(to: user.email) do |format|
format.html { render layout: 'my_layout' }
format.text
end
end
end
This will render the HTML part using the my_layout.html.erb file and the text part with the usual user_mailer.text.erb file if it exists.
Please read here.
Thanks
Related
I am overriding devise session controler and while trying to set specific format Rails says there's no partial while it's not true
while I am trying to insert it inside respond_to
class SessionsController < Devise::SessionsController
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
respond_to do |format|
format.html
format.json {render(json: { sign_in: render_to_string(partial: 'static_pages/home')})}
end
end
The error is:
Missing partial static_pages/_home with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "D:/project_traveldiary/app/views" * "D:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/devise-4.6.2/app/views"
Looks like it searches for json partial, so try to explicitly specify the format of the partial:
render_to_string(partial: 'static_pages/home', formats: :html)
I'm using render_to_string as a json response. It works fine in my app, but rspec can't find the partial.
controller:
# controllers/admin_controller.rb
class AdminController < ApplicationController
def retrieve_edit_form
user = User.find(params[:id])
form = render_to_string('_user_form', layout: false)
respond_to do |format|
format.json { render json: { form: form } }
end
end
end
spec:
# spec/app/controllers/admin_controller_spec.rb
require 'rails_helper'
RSpec.describe AdminController, type: :controller do
render_views
describe 'GET retrieve_edit_form' do
it 'returns http success' do
get :retrieve_edit_form, id: 100, format: :json
expect(response).to be_success
end
end
end
I get the error:
Missing template admin/_user_form, application/_user_form with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
* "/Users/.../app/views"
How can I get rspec to recognize the partial?
Ok, I figured it out. Turns out I needed to add the extension to the partial and it all works fine.
The controller needed to be
form = render_to_string('_user_form.html.erb', layout: false)
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"},
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.
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