ActionView::MissingTemplate: Missing template :formats=>[:mobile] - ruby-on-rails

In Rails, I'm trying to implement mobile views. Just installed mobylette gem which seems to be have more recent activity than mobile_fu.
https://github.com/tscolari/mobylette
However, most of the requests from the mobile device end up with this error
ActionView::MissingTemplate: Missing template my_controller/index, application/index with {:locale=>[:en], :formats=>[:mobile], :handlers=>[:erb, :builder, :haml]}. Searched in: * "/app/app/views" * "/app/vendor/bundle/ruby/1.9.1/gems/devise-1.5.2/app/views" * "/app/app/views"
Any idea why?

You probably need to add the mobile format your self.
In config/initializers/mime_types.rb, add:
Mime::Type.register_alias "text/html", :mobile

If you set the mime type and have respond_to :mobile in your controller, your only problem should be the mobile template file actually missing.
By default, you need to have a template file in the appropriate view_path called <action>.<format>.<handler> or <action>.<handler>
I suspect the default devise template (after successful create redirects to show action) is show.html.erb
your safest bet is to create it or symlink it (and all other actions)
ln -sf show.html.erb show.mobile.erb # in linux
Where? Well, if you used devise rake task
rails generate devise:views
then you know it'll be under app/views/devise/registrations
If you set config.scoped_views = true inside config/initializers/devise.rb, then it'll be under app/views/users/registrations given your devise model is User using UsersController.

Related

Rails can't find view template even though it exists

This is the error I'm getting whenever I'm accessing localhost:3000/cats:
Missing template cats/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "/home/mikael/RubyOnRailsLearning/NinetyNineCats/app/views"
* "/home/mikael/.rvm/gems/ruby-2.6.3/gems/actiontext-6.0.2.1/app/views"
* "/home/mikael/.rvm/gems/ruby-2.6.3/gems/actionmailbox-6.0.2.1/app/views"
My view templates are located like so:
app/views-layouts/cats/index.html.erb, show.html.erb
I have also tried removing them from cats and into the views folder.
My index controller action is this:
def index
#cats = Cat.all
render :index
end
The routes.rb file has only this inside:
resources :cats
This project worked fine yesterday. It could find the templates and render them just fine. But I wanted to restart it so I deleted the rails app folder without dropping the database and I remade it today. The database schema got uploaded into the rails app.
I can't think of anything that might be causing this problem besides me not dropping yesterday's database and not remaking it. (it's the only difference)
You wrote that your view is in
app/views-layouts/cats/index.html.erb
but Ruby on Rails conventions expect it in
app/views/cats/index.html.erb

Missing template when uploading files with Paperclip (only when form is remote: true)

I am working on a Rails app. I have a form where the user can submit some files. The form is doing an AJAX call (it has the remote: true attribute). Whenever I want to send a file using the "f.file_field :banner" helper, the server responds with
Missing template posts/update with {:locale=>[:fr], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim]}. Searched in: ...
I have tried to add a respond_to block into my controller but then the server throws an ActionController::UnknownFormat.
Why is it not rendering my JS template ONLY when I submit a file? The form works fine when I just submit text.
Thank you for you help!
EDIT:
My form:
= bootstrap_form_for #section, remote: true do |f|
= f.file_field :banner
...
My controller:
# PATCH/PUT /sections/1
def update
#section.update(section_params)
manage_photos
render "posts/update"
end
Solution:
Using the remotipart gem !
Your problem is specifically the line in your controller:
render "posts/update"
in your views directory, do you have a file posts/update.html.erb or the like. That is what it is trying to render. It can't find the file. What are you trying to do with that line?
UPDATE:
Try doing this:
render 'posts/update', formats: [:js]

Rendering a view from my Ruby on Rails Gem

I created a simple Gem for Ruby on Rails; the idea is that it provides some code/views for common actions (index/show/etc.) that I use in several of my applications. I would like to "DRY it up" in a Gem.
Creating the Concern went without problems, however, I can't quite seem to manage to render a view in my application.
For example, in my lib/rails_default_actions/rails_default_actions.rb I do:
module RailsDefaultActions
module DefaultActions
extend ActiveSupport::Concern
respond_to do |format|
format.html { render 'default/index' }
end
end
end
end
But an error is raised:
Missing template default/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :haml]}. Searched in:
* "/home/martin/myapp/app/views"
* "/home/martin/myapp/vendor/bundle/ruby/2.1.0/gems/devise-3.2.4/app/views"
* "/home/martin/myapp"
I eventually sort of managed to hack my way around this error, but it feels very kludgey and doesn't work in some scenarios. What is the correct way to include views in a gem?
I looked at creating an Engine, but that seems like overkill, since I just have a concern and a few views.
The "correct" way to do this since Rails 3 is to create an Engine; there's also a Rails guide for this, but creating a basic engine is as easy as:
module MyGemName
class Engine < Rails::Engine
end
end
When Rails looks for a view to render, it will first look in the app/views directory of the application. If it cannot find the view there, it will check in the app/views directories of all engines that have this directory.

Is it necessary to build Rails views when using Ember.js?

I'm updating a Rails app to utilize Ember.js. Those views that existed within the app prior to integrating ember still work fine, but I've also added several new views. These views have all the necessary ember parts (template, controller, etc), as well as all the Rails parts, excluding the view files.
These views work fine if the user accesses them by clicking on an internal link. However, if the user reloads the page or manually enters the URL, then I get this error:
ActionView::MissingTemplate at /contribute
Missing template pages/contribute, application/contribute with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :coffee]}. Searched in:
* "/home/sites/whistlr/app/views"
* "/home/.rvm/gems/ruby-2.0.0-p0#whistlr/gems/devise-3.0.0/app/views"
This is clearly happening because I do not have view files. The question is, is this strictly necessary? Is there some way to tell Rails to just load up the Ember views? Ideally, I'd just delete all the old Rails view files once the conversation is complete.
It isn't necessary, but you need to setup the rails routes.rb to have a catch all route that also renders just like your index page which displays the ember app and its html.
namespace :api do
# resources go here
end
root :to 'home#index'
match "/*path" => 'home#index'
Note: You want to customize this path pattern to your project, else 404s would also be send here.
It's not necessary to create individual views. The trick is to catch the exception in the application controller and then force it render the layout:
class ApplicationController < ActionController::Base
rescue_from ActionView::MissingTemplate do |exception|
render "/layouts/application"
end
end

ActionView::MissingTemplate: Missing template (Trying to render nonexistent :mobile format )

I have an index.js in place which is fired ok from desktop but gives the following error when I try to open from Mobile Safari.
ActionView::MissingTemplate: Missing template ads/thumbs/index, application/index with {:locale=>[:es, :en], :formats=>[:mobile], :handlers=>[:erb, :builder, :haml]}. Searched in: * "/app/app/views" * "/app/vendor/bundle/ruby/1.9.1/gems/devise-1.5.2/app/views" * "/app/app/views"
I'm not sure why it is looking for a :formats=>[:mobile] when there's no such a file in the folder.
Same thing happens after trying to sign in with devise from mobile phone. I tries to render a nonexistent create.mobile.haml file.
P.S. Is there a way to make :mobile views to fallback default :html views when not found? That would make the trick.
You should in general respond with content-type specific views. In this case, a simple way to get past this short-term issue is to rename index.js to index.mobile.js.
Rails attempts to render views that are specific to the content-type requested -- for example, index.html.haml when html is requested or show.json.haml if you request json. In this case the content-type requested is :mobile.
In the long run you should develop views that will be sent back when different content types are requested.
Here's a simple solution.
class ApplicationController
...
def formats=(values)
values << :html if values == [:mobile]
super(values)
end
...
end
It turns out Rails (3.2.11) already adds an :html fallback for requests with the :js format. Here's ActionView::LookupContext#formats=
# Override formats= to expand ["*/*"] values and automatically
# add :html as fallback to :js.
def formats=(values)
if values
values.concat(default_formats) if values.delete "*/*"
values << :html if values == [:js]
end
super(values)
end
So you can override #formats= yourself and it will be conceivably no more gross and hacky than the existing Rails implementation.

Resources