Rails missing template - ruby-on-rails

I get this when I try and run my view:
Missing template application/login with {:formats=>[:html], :locale=>[:en], :handlers=>[:coffee, :erb, :builder]}. Searched in: * "/home/carladessi/Goods In Final/app/views"
in my controller I have:
def login
# respond_to do |format|
# format.html
end
and in my routes I have:
match "/login/", :controller => 'application', :action => 'login'
I'm guessing I need to put something else in the controller I just don't know what.. sorry if this is a really blatant question!

Restarted the server and it works fine !

It is not really conventional Rails to render views from the application_controller.
However, what is happening is Rails is looking for an actual template or view to be here:
RAILS_ROOT/app/views/application/login.html.erb
What you can do is add/create that template at the above path. Or you can redirect to another controller (which exists and does render an actual template).

Related

Rails can't see .rjs file

I'm trying to learn how to use AJAX in my rails apps so i've decided to start with something simple. I have a blog app on which user can vote on any blog post. Here is my code for posts#vote:
posts_controller.rb
class PostsController < ApplicationController
(...)
def vote
post = Post.find(params[:id])
if current_user.voted_on?(post)
current_user.unvote_for(post)
else
current_user.vote_for(post)
end
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js
end
end
end
and here is a link code for my posts#view:
view.html.erb
<%= link_to "Vote", vote_post_path(post.id), :remote => true %>
And now, if i'll click my Vote link, posts#vote action works and vote is casted, however i'm getting an error:
ActionView::MissingTemplate (Missing template posts/vote,
application/vote with {:handlers=>[:haml, :coffee, :erb, :builder],
:locale=>[:en], :formats=>[:js, :html]}.
I have (empty) vote.rjs file in my views/posts folder but for some reason rails can't see it. According to error, the only file extensions that rails is searchng for are .haml, .coffee, .erb and .builder. Shouldn't there be also a .rjs extension on that list? Thanks in advance.
Your file should be called vote.js.erb. Rails doesn't use a .rjs extension.
The .rjs extension was originally used for Rails and Prototype JS library.
With Rails 3.1 the default library was switched to JQuery.
If you want to use .rjs in your Rails project it would mean using Prototype instead of JQuery. The gem for that is prototype-rails.

Rendering partials not working from controller

I've got a home_controller.rb that has different methods whose purpose is to just render render partials that have different content.
I have the urls created in the config
get '/interview', :to => 'home#interview'
get '/chambers', :to => 'home#chambers'
get '/letter', :to => 'home#letter'
get '/drafting', :to => 'home#letter'
and the methods set up that render partials (I was experimenting with symbols vs single quotes) from the home_controller.rb
def chambers
render 'home/chambers'
end
def drafting
render 'drafting'
end
def interview
render :interview
end
However, I'm getting a missing template error (Template is Missing) message when I click the links.
I can get it to work without using partials but rather regular files interview.html.erb (for example) but I would still like to know why it's not working with partials.
Thanks
you need to specify it as partial if you want to render a partial else rails will look for file or an action with that name.
render :partial => 'drafting'
Code for rendering logic in rails https://github.com/rails/rails/blob/5215eed5a3f18c76d70f0f25bca4ff6286c4bac8/actionpack/lib/abstract_controller/rendering.rb#L141

Setting view formats for custom mimetypes in Ruby on Rails?

I'm creating a custom mimetype in rails to use with respond_to
Mime::Type.register_alias "text/html", :modal
I want to use this mime type in respond_to like so:
respond_to do |format|
format.html{ render 'index'}
format.modal{ render 'index', :layout => 'bare'}
end
I want this format to basically serve the same views as the .thml format but with a different layout
I'm getting a missing template error
Missing template support/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:modal], :locale=>[:en, :en]} in view paths "/remote/app/views",
its looking for a view named index.modal.erb
i currently only have index.html.erb
I've tried creating the index.modal.erb and it does work but then the layout has the same problem i only have a layout at bare.html.erb
I really don't want to duplicate these view files for the different mime types. I'm hoping theres a way to have the custom mime type fall back on to html views that i am missing.
I am going by your requirement that
I want this format to basically serve the same views as the .html format but with a different layout
I have a different approach that should work in Rails 3 (tested in Rails 3.2.12). Put the following in your controller:
before_filter do
#bare= (params[:format] == 'modal')
if #bare
params[:format]= 'html'
request.format= :html
end
end
layout :select_layout
# standard controller stuff
# ...
# Towards bottom of your controller code,
private
def select_layout
#bare ? 'bare' : nil
end
Summary:
Change the format back to html.
Set a variable to indicate that it needs to serve 'bare'
Tell controller that value of layout will be given by a function. (See 'Choosing Layouts at Runtime' in http://guides.rubyonrails.org/layouts_and_rendering.html)
No changes needed in any of the respond_to blocks.
This will do exactly what you need, i.e., same action as .html but with a different layout.
What version of Rails are you using? If 3.2, try setting the format explicitly for your modal response:
respond_to do |format|
format.html { render 'index'}
format.modal { render 'index', :formats => [:html], :layout => 'bare'}
end
That should make it render index.html.erb instead of index.modal.erb.

Rspec test fails missing template format issue

I am trying to implement some integration tests for my application to test a voting system I have in place but have run into some problems. First off, here is the test code I am trying to get to pass:
describe "vote_up_user" do
it "should update the user rating" do
click_link "user_up_arrow"
response.should have_selector("#user_rating", :content => "1")
end
end
Here is the link that gets clicked:
<%= link_to image_tag("uparrowbig.png"), vote_up_user_path(#user), :method => :post,
:id => "user_up_arrow", :class => "arrow", :remote => true %>
The corresponding action:
respond_to :html, :js
def vote_up_user
#voted_on_user = User.find(params[:id])
current_user.vote_exclusively_for(#voted_on_user)
respond_with(#voted_on_user, :location => user_path(#voted_on_user))
end
and in case anyone is interested the corresponding votes/vote_up_user.js.erb:
$("user_rating").update('<%= #voted_on_user.plusminus.to_s %>')
$("user_up_arrow").update('<%= image_tag("uparrowbigselect.png") %>')
$("user_down_arrow").update('<%= image_tag("downarrowbig.png") %>')
My problem is that I keep failing at the click_link line with the following error:
Missing template votes/vote_up_user with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]}
I can understand why this is failing as I do not have a template that is html.erb in the specified path. I actually have a js.erb file instead as this is an AJAX call, but this is not included in the :formats array and thus is not found. My question is then, what would be the best way to ensure that the :js format is search for when the integration test clicks on the link? Is this something I can simply adjust in the test or will I need to add it to the link helper?
I suspect the reason it's not working is that RSpec alone can't test Javascript.
When you add remote => true to a link, it only adds data-remote="true" as an attribute of the link, which doesn't mean anything without Javascript. That's why you see in your error :formats=>[:html]. It is only going to look for html views. In order for Rails to request the .js.erb view by default, you either need to have the .js on the end of the URL that it is requesting or actually use Javascript to request the page.
To get Javascript to actually run in your tests, you need to use something like Capybara. When you run your test, you'll actually see your browser start up and it will run your test actually in the browser.
If this is what you want to do, I would recommend watching Ryan Bates' recent Railscast: http://railscasts.com/episodes/257-request-specs-and-capybara
Update based on comments
respond_with will only redirect to the location you specify on POST, PUT, or DELETE requests. While you have :method => :post in the link, links will always generate GET requests when Javascript is disabled (since you're not using AJAX). Without Javascript, the only way to generate a POST request is with a form tag.
If you want it to degrade gracefully in this situation, you should either create an html view for these situations or put a block after the respond_with like this:
respond_with(#voted_on_user, :location => user_path(#voted_on_user)) do |format|
format.html { redirect_to user_path(#voted_on_user) }
end`

rails controller test, getting ActionView::MissingTemplate: Missing template

I figured I should finally write some tests for my rails app.
My controller is "UsersController". It doesn't have any HTML as I just have an iphone app sending a post in to a rails controller.
Here is my test:
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def test_create
# to http post
# /users
#user[email]=%#&user[password]=%#&user[password_confirmation]=%#
#post
post(:create, :user => {:password => "testpassword", :password_confirmation => "testpassword"})
end
Problem is that I get this error:
1) Error:
test_create(UsersControllerTest):
ActionView::MissingTemplate: Missing template users/new with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths
So I guess it's trying to populate an HTML page? If so, I find this odd. I would think it would directly do the post to the controller. Can someone confirm that this "post" method tries and populates an HTML form?
If this is the case, how should I proceed in writing a test to directly send an HTTP post to the controller?
Thanks for any help
You can specify "format" to make it work:
post(:action, {'param1'=>'value1', 'param2' => 'value2', :format => 'js'})
Unless you tell it otherwise the post method assumes the requested content type is HTML. Typically the create action looks something like this:
#user = User.new(params[:user])
if #user.save
redirect_to posts_path
else
render :new
end
If the action fails it tries to render 'users/new', which doesn't exist, thus the error.
So there are a couple of issues here. It's not clear what content type is expected (XML?). The action is failing, but we can't see why. It might help to edit the question to show us what the controller action does.

Resources