Jbuilder without the .json extension - ruby-on-rails

Doing http://localhost:3000/options/audio
And I get error:
Missing template options/audio, application/audio with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/Users/mmahalwy/Desktop/Code/quran.com/QuranAPI/app/views"
When I do http://localhost:3000/options/audio.json
That renders my Jbuilder file and the desired json. How can I render my jbuilder template with the .json extension?

The jbuilder documentation tells you to create your jbuilder template as a *.json.jbuilder file. This makes the route look for the .json extension. If you remove the .json part and make your file *.jbuilder you will be able to go to the route without the .json extension.
Example:
Filename: index.jbuilder
route: get 'ticket', to: 'tickets#index'

jbuilder is a gem, a tool.... I haven't tried it but if you want default functionality (html) with json and jbuilder, then:
http://localhost:3000/options/audio
will look for
/Users/mmahalwy/Desktop/Code/quran.com/QuranAPI/app/views/.../audio.html.jbuilder
So, you can put your code in there... But it can only be jbuilder code... Otherwise, you can use a plain html.erb or other file with embedded ruby and you will specify jbuilder json creation in it:
Jbuilder.encode do |json| # this is implied in .jbuilder files but can be used in any other ruby file
json.xxx #yyy.xxx
#....
end

Related

Missing template error for Ruby on Rails Application

I have a view in the LoginController with the following code
def home
render 'home'
end
However, whenever I load my web server, it shows the error message:
ActionView::MissingTemplate (Missing template login/home, application/home with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Users/60984/prairielearn-eecs/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actiontext-6.1.7.2/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actionmailbox-6.1.7.2/app/views"
):
app/controllers/login_controller.rb:16:in `home'
Even though I have the file in the directory app/views/login/home.html.erb.
I tried changing it to a more specific directory such as
def home
render 'login/home'
end
but it still fails. However, I tried using the whole path like
render file: 'C:/Users/.../app/views/login/home.html.erb'
With the code above, the problem changed into the html.erb is not being interpreted, it will display the code blocks on the browser no matter what browser I use.
I am just confused about why it doesn’t work since I have the html file in the designated place.

Render a partial in gem views folder

I'm writing a gem and I need that the app that use the gem could render a partial using = render "user_role_select" from the layout/application.html.haml.
My _user_role_select.html.haml lives in the gem at the path app/views/gem_name/. When I'm calling the render I'm getting the followed message:
Missing template /user_role_select with {:locale=>[:en],
:formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html,
:builder, :ruby, :haml]}. Searched in: * "/ruby-app/app/views" *
"/bundle/gems/bootstrap-kaminari-views-0.0.5/app/views" *
"/bundle/gems/kaminari-core-1.0.1/app/views" * "/ruby-app" * "/"
I'm noticed that the kaminari folder for example is included in the folders in which rails search so I'm guessing this is possible, but even looking in them source code I couldn't find any hint.

Template missing in render :file in ajax call when the file exists in Rails 4.2

Ajax is used for Bootstrap modal call. The js.erb file is called successfully and this js.erb file then should load html.erb residing in the same subdir as the js.erb is under biz_workflowz/app/views/application/. Here is the js.erb file:
$("#newworkflow .modal-content").html('<%= j render(:file => "/biz_workflowx/application/event_action.html.erb") %>');
$("#newworkflow").modal();
However the event_action.html.erb is never found. The error is raised in action_view/path_sets.rb:
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args)) #find_all(*args).first returns NIL
end
The error is:
ActionView::Template::Error (Missing template c:/d/code/rails_proj/engines/biz_workflowx/app/views/application/event_action.html.erb with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
.......
* "C:/D/code/rails_proj/engines/biz_workflowx/app/views" #<<== did search the subdir but did not find anything
* "C:/D/code/rails_proj/engines/searchx/app/views"
* "C:/D/code/rails_proj/engines/commonx/app/views"
* "C:/D/code/rails_proj/engines/authentify/app/views"
* "C:/D/code/rails_proj/webportal"
* "C:/"
):
1: $("#newworkflow .modal-content").html('<%= j render(:file => "biz_workflowx/application/event_action.html.erb") %>');
2: $("#newworkflow").modal();
actionview (4.2.0) lib/action_view/path_set.rb:46:in `find'
Even with hardcoded path to the file event_action.html.erb, it still returns template missing. The similar js.erb code has been used in a few places for ajax call and I does not see why here it does not work. What could cause this error?
Updated
Here is the debug windows showing the biz_workflowz/app/views has been searched by resolver which does not see the file under /application/. Strange!
add event_action.html.erb file under c:/d/code/rails_proj/engines/biz_workflowx/app/views/application

Rails doesn't seem to be looking for partial in the same directory

I'm working on exercise 3 in Chapter 10 of Michael Hartl's The Ruby on Rails Tutorial.
I'm refactoring code in app/views/static_pages/home.html.erb and moving it into the two partials:
app/view/static_pages/_signed_in.html.erb
app/view/static_pages/_non_signed_in.html.erb
and calling render on both files. For some reason I get this error:
ActionView::Template::Error:
Missing partial static_pages/non_signed_in, application/non_signed_in with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.
However, when I move the two partials to the sibling directory app/view/static_pages/shared and tell render to look there (i.e. "render 'shared/signed_in'" and "render 'shared/non_signed_in'"), the error disappear. Thoughts?
Thanks!!

ActionView::MissingTemplate: Rails not looking for JSON format

I'm using Backbone.js and thus bootstrapping data using a JSON (jbuilder) partial like so (using HAML):
App.users = new App.UserList(#{render('users/index', :formats => [:json], :handlers => [:jbuilder], locals: {users: #users})})}, {silent:true});
It gives me this error:
ActionView::Template::Error (Missing partial users/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder, :arb, :coffee, :haml]}. Searched in:
Notice that it is only looking for the ':html' format, despite me passing in 'formats: [:json]'. Should I be doing something differently?
Thank you for any help.
What version of Rails are you using? This problem was fixed for 3.2.3, but exists in earlier versions.
For a quick fix, though it will cause deprecation warnings in Rails 3.2 and later, you can add the format to the name of the template, i.e. render('users/index.json' ...

Resources