I want to learn about webapps. I decided to learn by doing and chose to start simple with Camping as (i). it is small & (ii). i know some ruby.
The favicon is not showing. I am using a favicon taken from another site so it know its file format is valid.
Here is the code from the controller.
class Favicon < R '/favicon\.ico'
# Load the favicon into memory
FAVICON = File.read('favicon.ico')
def get
#headers['Content-Type'] = "image/x-icon"
FAVICON
end
end
Here is the code from the view:
I purposely placed a link to the favicon twice as an experiment. No joy.
def layout
html do
head do
title 'Custom Made Kameez'
link :rel => 'icon', :href => 'favicon.ico', :type => 'image/x-icon'
link :rel => 'shortcut icon', :href => 'favicon.ico', :type => 'image/x-icon'
link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen'
end
I have tried clearing my cache and using Firefox and IE, same issue.
Two problems as far as I can see.
You should use an absolute path to the favicon:
link :rel => 'icon', :href => '/favicon.ico', :type => 'image/x-icon'
You should read the file as binary:
FAVICON = File.binread('favicon.ico')
I took your code and it worked just fine, and that was before I uncommented the link directives: browsers are just really keen to go get /favicon.ico .
Is your problem the fact that your favicon never showed in the first place, or that your original favicon is now stubbornly refusing to change?
Related
I want to convert my embedded ruby file to PDF file after clicking on a link using Rails 3.I became able to convert simple html file to PDF file using pdfkit gem.I am explaining my code below.
users_controller.rb:
class UsersController < ApplicationController
def index
end
def download_pdf
#html = render_to_string(:action => "/users/download_pdf.html.erb")
#kit = PDFKit.new('http://google.com')
#kit = PDFKit.new(html)
#send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
kit = PDFKit.new("<h1>Hello</h1><p>This is PDF!!!</p>", :page_size => "A4")
send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
#file = kit.to_file('my_file_name.pdf')
end
end
In this controller page i did and got success to convert from HTML to PDF.
users/index.html.erb:
<p>
<%= link_to "Download pdf",download_pdf_path(:format => 'pdf') %>
</p>
When user will click on the above "download_pdf" link the download.html.erb will convert to PDF file and it should display as well as download in specified folder.The download.html.erb file is given below.
users/download.html.erb:
<h1>Hello Rails</h1>
The above file should convert into PDF file with proper css .If i have css for this like below.
application.css:
h1{
width:100px;
height:100px;
background-color:red;
}
How can i include this CSS in that PDF file.My other files are given below.
pdfkit.rb:
PDFKit.configure do |config|
#config.wkhtmltopdf =Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
config.wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe'
#config.default_options[:ignore_load_errors] = true
end
Please help me to resolve this issue and make this successfully.
By using wicked_pdf gem i am getting the following error.
error:
RuntimeError in UsersController#download_pdf
Error: Failed to execute:
["C:/wkhtmltopdf/bin/wkhtmltopdf.exe", "file://C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-3204-calx6j.html", "C:/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf_generated_file20150527-3204-59mbli.pdf"]
Error: PDF could not be generated!
Command Error: Loading pages (1/6)
[> ] 0%
[======> ] 10%
Error: Failed loading page file://c/DOCUME~1/SUBHRA~1/LOCALS~1/Temp/wicked_pdf20150527-3204-calx6j.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentNotFoundError
check the below code for this gem.
users/users_controller.rb:
class UsersController < ApplicationController
def index
end
def download_pdf
render pdf: 'test',
layout: '/layouts/test',
template: '/users/test',
handlers: [:erb],
formats: [:pdf],
:save_to_file => Rails.root.join('public', "test.pdf")
end
end
wicked_pdf.rb:
WickedPdf.config = {
#:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
#:layout => "pdf.html",
:exe_path => 'C:/wkhtmltopdf/bin/wkhtmltopdf.exe'
}
You can include your stylesheets by following:
def download_pdf
kit = PDFKit.new(File.open(Rails.root.join('app', 'views', 'users', 'download.html.erb')))
kit.stylesheets << Rails.root.join("app","assets","application.css")
send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')
end
I have reviewed a number of solutions from stackoverflow to no avail. I was hoping someone could help me understand the error of my ways.
The haml markup looks like:
.graduation-date-range
%label Graduation Date Range
.row-fluid
.span6
.control-group
.controls
.bfh-selectbox{:id => "graduation_start_year", :style => "width: 100%"}
= f.hidden_field :graduation_start_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option Start Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.start-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
.span6
.control-group
.controls
.bfh-selectbox{:style => "width: 100%"}
= f.hidden_field :graduation_end_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option End Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.end-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
The solution I am currently trying to employ:
select('2012', :from => 'graduation_start_year', :visible => false)
I receive :
Capybara::ElementNotFound:
Unable to find select box "graduation_start_year"
My understanding is that either the label, name, or ID can be used to select from but I may be mistaken.
Thank you in advance to those who respond.
It appears that this is not a form select box, but an html list that has been turned into something that resembles a dropdown box via javascript and CSS.
capybara select is looking for <select> and <option> tags. This is going to require a js type test with selenium or poltergeist to test.
Hi guys I remove /page from high_voltage gem with this answer.
Remove page/ of High Voltage for statics page rails
I have in my routes for high_voltage this:
match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
For maintenance page 404 in rails 3.1 I follow this fix http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution with errors_controller.rb with the next code:
def routing
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Then I add to routes.rb the next code for maintenance page 404 in rails
match '*a', :to => 'errors#routing'
The problem is that if I put in browser www.mydomain.com/sdfs dont working 404 system error and show No such page: sdfs
but however if I put www.mydomain.com/a_controller/action/sdfs yes working fine the fix for error 404 page.
I think that problem is my routes.rb
I solved this problem by extending the HighVoltage::PagesController and modifying the error catching:
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
In my case though, my 404 function resides in my application controller so that it can easily be called from any location. If you make the same change, you will also need to update your route:
match '/:id' => 'pages#show', :as => :static, :via => :get
Thank you kevinthopson for mi this dont working fine :(.
I have in my routes.rb:
match '/:id' => 'pages#show', :as => :static, :via => :get
I have added this in pages_controller.rb
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
I have added this code to aplication_controller.rb:
def render_not_found
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Working fine if you put now:
localhost:3000/dfadsfadsf
The problem now is that if you put for example that routes in navigation bar:
localhost:3000/users_or_static_page/asdfadfadfa
Dont working for me :(.
I've got a Rails3 mailer layout that include images.
This ones are used like :
image_tag("emails/top.gif", :width => "700", :height => "10", :alt => "")
As of Rails 2, this images included the host and produced the expected result. However, since Rails3 the config.action_mailer.default_url_options seems to be ignored.
Is there anything I'm missing?
Update
my config/environment/development.rb include:
config.action_mailer.default_url_options = { :host => 'mydomain.tld' }
Needs to use config.action_mailer.asset_host = 'http://mysite.com' in your environment config file
Credits: wmoxam in #rubyonrails
I have a resource defined in my routes.
resources :categories
And I have the following in my Category controller:
def show
#category = Category.find(params[:id])
respond_to do |format|
format.json { render :json => #category }
format.xml { render :xml => #category }
end
end
The controller action works fine for json and xml. However I do NOT want the controller to respond to html format requests. How can I only allow json and xml? This should only happen in the show action.
What is the best way to achieve this?
Also is there any good tips for DRYing up the respond_to block?
Thanks for your help.
I found that this seemed to work (thanks to #Pan for pointing me in the right direction):
resources :categories, :except => [:show]
resources :categories, :only => [:show], :defaults => { :format => 'json' }
The above seems to force the router into serving a format-less request, to the show action, as json by default.
You must wrap those routes in a scope if you want to restrict them to a specific format (e.g. html or json). Constraints unfortunately don't work as expected in this case.
This is an example of such a block...
scope :format => true, :constraints => { :format => 'json' } do
get '/bar' => "bar#index_with_json"
end
More information can be found here: https://github.com/rails/rails/issues/5548
This answer is copied from my previous answer here..
Rails Routes - Limiting the available formats for a resource
You could do the following in your routes.rb file to make sure that only the show action is constrained to json or xml:
resources :categories, :except => [:show]
resources :categories, :only => [:show], :constraints => {:format => /(json|xml)/}
If this doesn't work you could try explicitly matching the action:
resources :categories, :except => [:show]
match 'categories/:id.:format' => 'categories#show', :constraints => {:format => /(json|xml)/}
constraints was not working for POST requests and then I tried defaults it works for all.
namespace :api, :defaults => { :format => 'json' } do
namespace :v1 do
resources :users do
collection do
get 'profile'
end
end
post 'signup' => 'users#create'
post 'login' => 'user_sessions#create'
end
end
I was using Rails 4.2.7