Rendering a partial with ajax - missing view - ruby-on-rails

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') %>" );

Related

rails upgrade render text gives missing template error

When upgraded the rails application from rails 5.0 to rails 5.2 I am getting the following error when I am trying to login using saml
Missing template sso/authsetup/saml, application/saml with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :axlsx, :haml, :jbuilder]}. Searched in: * "/home/app/be/app/views/custom/1" * "/home/app/be/app/views" * "/usr/local/bundle/gems/ruby/2.5.0/gems/letter_opener_web-1.4.1/app/views" * "/usr/local/bundle/gems/ruby/2.5.0/bundler/gems/ckeditor-bbc03cf0b757/app/views" * "/usr/local/bundle/gems/ruby/2.5.0/gems/kaminari-core-1.2.2/app/views" * "/usr/local/bundle/gems/ruby/2.5.0/gems/bootstrap5-kaminari-views-0.0.1/app/views"
I am getting this error on this line
render :text => "Omniauth setup phase.", :status => 404
I have gone through a few of the https://github.com/thoughtbot/ember-cli-rails/issues/527 & What to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later? and try to update this line
render :plain => "Omniauth setup phase.", :status => 404
head :not_found
respond_to do |format|
format.html { head :not_found }
end
But none of it works. Any help is appreciated thanks

respond_to with format.json doesn't find partials

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)

Render js.erb from controller

I have a controller action that I am trying to only render js from. Here's what I have
def get_script
respond_to :js
render :script
end
I'm using respond_to :js to hopefully force the request to only respond to js. Then I'm calling render :script to load a file called script.js.erb
My request is the following
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.5ms)
ActionView::MissingTemplate (Missing template widgets/get_script, application/get_script with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder]}. Searched in:
* "/home/jkoehms/TECC/tecc/app/views"
* "/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/devise-3.5.1/app/views"
)
So there are two problems here. Although the request is processed as JS, the render is looking for js or html as indicated in the formats section. Secondly, the render is looking for get_script.js.erb when it should be looking for script.js.erb. I used the following documentation as a resource for rendering: Layouts and Rendering
Question:
1.) Does the respond_to :js do what I'm hoping it to do, or do I have to put it in a do |format| block?
2.) Why isn't render :script looking for script.js.erb?
Have you tried this inside your view?
render "widgets/script.js.erb", format: :js

.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.

Rendering an action outside the current view folder

I am using Ruby on Rails 3.0.9 and jQuery 1.6.2. I would like to "render an action file" from a folder not in the current view file used\displayed.
In my articles_controller file I have:
respond_to do |format|
format.html {
# ...
}
format.js {
render :action => 'shared/article.js.erb' # Note the 'shared' folder
}
end
Making an AJAX HTTP request from the article view (the view is the show.html.erb file and it is located in views/articles folder) I get the following error:
ActionView::MissingTemplate (Missing template articles/article with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]}...
How can I solve that?
The log file is the following:
Started GET "/articles/2/article" for 127.0.0.1 at 2011-07-24 13:05:16 +0200
Processing by ArticlesController#article as JS
ActionView::MissingTemplate (Missing template ActionView::MissingTemplate (Missing template articles/article with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]}...
On Rails 3, this should work:
render 'shared/article'
If it does not, you can be explicit:
render :template => 'shared/article'
More about this on the rendering chapter at Rails Guides.

Resources