respond_to with format.json doesn't find partials - ruby-on-rails

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)

Related

rails 5.1 API and Jbuilder render problem

After setup a simple API and try to login with a simple session controller
Session Controller
def create
user = User.find_by(email: [:email])
if user && user.authenticate([:password])
render json: {token: user.access_token}, status: 200
else
render text: "Email and password combination are invalid", status: 422
end
end
in the logs show
ActionView::MissingTemplate (Missing template sessions/create,
application/create with {:locale=>[:en], :formats=>[:json],
:variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby,
:jbuilder]}. Searched in: * "/home/daniel/ranks/app/views" ):
after googling around seems a issue with jbuilder and the workaround that some proposed was to: in the application controller
include ActionView::Rendering
def render_to_body(options)
_render_to_body_with_renderer(options) || super
end
but the problem persist so someone has any idea how to solve this issue with rails 5.1 and jbuilder 2.7.0?

Rendering a partial with ajax - missing view

I have commendations/_create.html.erb which includes in a form -
<%= form_for(current_user.commendations.new(...), remote: true) do |f| %>
<%=...
and in commendations_controller.rb
def create
...
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
but I'm getting the following error in the server log -
Completed 500 Internal Server Error in 7ms
ActionView::MissingTemplate (Missing template commendations/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/Users/dan/Documents/ROR/fic/app/views"
* "/Users/dan/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.4.1/app/views"):
app/controllers/commendations_controller.rb:9:in `create'
I can't see where I'm going wrong - the remote: true should trigger format.js which in turn should reload the partial, no?
In routes.rb I have
resources :commendations, only: [:create]
You need a file called create.js within app/views/commendations/
And in create.js do:
$("#your_div").html( "<%= j render( :partial => 'commendations/your_partial') %>" );

.json.rabl not showing in view for Rails app

Trying to get User model to print to JSON, tried making a file with a json.rabl extension as follows:
file.json.rabl
object #users
attributes :id, :name
When I try to view this, I get the following error message:
Missing template users/autocomplete, application/autocomplete with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :rabl, :rb]}.
However, if I rename the file to file.rabl, it renders the JSON, but in an HTML page instead. How do I get it to print a pure JSON file?
Here is my controller code for the file view:
def file
#users = User.all
respond_to do |format|
format.html
format.json
end
Just had to add .json to the URL.

rails 4 template missing error in render in ajax

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"},

action mailer gives error while passing layout

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

Resources