Cannot find application.html.haml in a new Rails 3 app - ruby-on-rails-3

I want to create Rails 3 app just to update myself, but I have strange issue.
I have created welcome#index action using
rails generate controller welcome#index
(I have haml-rails gem) and I observe that a new view index.html.haml has been created. This action is my root_path, so in localhost:3000 I get content of this view.
I had application.html.erb which I have changed manually to application.html.haml and I have added layout 'application' in ApplicationController, but I get
Template is missing
Missing template layouts/application with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}
now, any ideas why ?
Maybe some code snippets:
class ApplicationController < ActionController::Base
layout "application"
protect_from_forgery
end
the file is in place:
mkk:~/projects/rails/gifts/app/views/layouts$ ls
application.html.html

Your ls shows the layout file is called application.html.html, change it to application.html.haml.

Related

Problems signing out with Devise in Rails 4

I'm having problems signing out in my app.
The error that it's giving me is:
Template is missing
Missing template users/destroy, application/destroy with {:locale=> [:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Searched in: *"/home/gregb/workspace/Sandbox/site/app/views"
* "/home/gregb/.rvm/gems/ruby-2.2.0/gems/devise-i18n-views-0.3.6/app/views"
* "/home/gregb/.rvm/gems/ruby-2.2.0/gems/devise-3.5.2/app/views"
Any help would be greatly appreciated.
If you're using devise, then that doesnt seem to me like the directory where the view exists.
did you do
rails g devise:views
or create your custom views?
there's no destroy.html.erb file in devise.
but if you want to create one , I'd suggest you to create the destroy action in a newly created UsersController, then in the routes you have to place the
resources :users, only: [:destroy]
under the
devise_for :users
in order for it to be accessible. Then rake routes and find the right route. That should create the destroy action route for you.Create views in the app/views/users/destroy.html.erb and that should hopefully do it for you.

Rails is responding with wrong format

I have the following action:
def something
# do something
respond_to do |format|
format.js
end
end
I also have an something.html.erb template in my controller.
When I GET /controller/something, it returns nothing.
When I GET /controller/something.js, it renders the html with no layout.
When I XHR GET /controller/something, it renders the html with no layout.
I would expect it to "return a template not found error" error instead of rendering the html template.
Is this a bug of Rails or am I doing something wrong?
rails v3.0.4
ruby v1.9.3
I think it's because of the “convention over configuration” principle. If you don't specify a render or redirect_to in the action of the controller, rails renders the view with the same name as the method. Look at Rails render guide

Render a page as a saved HTML file using Rails 3.0?

I'm building a simple website generator application in Rails 3.0. I'd like a "publish" action in a controller that works just like an ordinary "show" action, but instead, saves a page as an HTML file in the "public" directory instead of displaying it in the browser. That is, I'd like to use the Rails render mechanism to create a file instead of providing an HTTP response.
What's the best way to do this?
Should I simply add caches_page :publish to the controller?
Or use render_to_string and File.new?
You can use render_to_string method: http://apidock.com/rails/AbstractController/Rendering/render_to_string
You still need to respond from the controller though. Maybe redirect to the page you just saved?
I would go with page caching.
Then if you have editable content, the pages should be automatically generated. You could then write a system task which bundles them up as a web site.
see (whatever)/actionpack/lib/action_controller/caching/pages.rb for instructions.
I found that page caching using caches_page won't work for me because there is no way to show a notification or redirect to another page after caching the page. The method render_to_string (suggested by #Grocery) is the way to go. Here's the example controller code:
def publish
#article = Article.find(params[:id])
html = render_to_string(:template => "articles/template.html.haml", :layout => 'article' )
FileUtils.makedirs("#{Rails.root}/public/articles/") unless File.exists?("#{Rails.root}/public/articles/")
File.open("#{Rails.root}/public/articles/#{#article.filename}.html", 'w') {|f| f.write(html) }
respond_to do |format|
format.html # publish.html.erb
end
end

Rails app doesn't see my views

I've on a while on rails now and here's the problem I've been having on and on:
When I create a controller through:
"rails generate controller ControllerName ViewName"
I get everything working as I want but if for some reason I create the controller through:
"rails generate controller ControllerName"
and then just add ViewName.html.erb to the folder inside views that has the same name as my controller things would go wrong.
So the concrete case is me writing:
rails generate controller Subjects list show.
Which creates for me:
1.controllers>subjects_controller.rb
2.views>subjects>list.html.erb
3.views>subjects>show.html.erb
So this whole thing works fine.But as I already said if I need another view; let's say "new" I just add "new.html.erb" next to the other *.html.erb files and an action:
def new
end
to my subjects_controller.rb then it won't work.
The two previous views would keep working but any other "*html.erb" created outside the command line wouldn't.
Is there anywhere else where info about views is being stored?.
I'm a Windows 7 user (32 bit).Rails version=3.0.3. WebServer=WEBrick.
Text editor = E-TextEditor
This is most likely caused by your routes not being correctly configured. So it would be helpful to see the content of your routes.rb
In your case I think the best way to configure the routes is to use the resources mapping:
resources :subjects
This will by default create routing for the standard RESTful actions :index, :show, :edit, :update, :new, :create and :destroy.
For more detailed information about the routing, I would recommend Rails Routing from the Outside In

Rspec2 and Rails 3 - View Spec Routing for Nested Resources

I have a nested resource, setup like this:
resources :chickens do
resources :eggs
end
The views for the EggsController are under views/eggs, but:
describe "eggs/index.html.erb" do
gives a "No route matches {:action => "create", :controller => "eggs"} error on calling render. How do I get RSpec to find the correct (nested) route for view specs?
As a side note, is there any way to specify a nested resource to the Rails 3 controller scaffold generator?
The test looks ok to me...
By any chance do you have a form on your eggs/index.html.erb for creating new eggs that might not yet be wired up correctly? It seems it may be trying to render the index view but failing because the view is trying build a route that doesn't exist? You'd want to make sure that the form is using the correct nested resource route. Does the view render when you load it up in the browser?