Hello!
I have this trouble: I'm searching reports by date and in html view everything is alright, BUT when I'm rendering xls view error appear, because it didn't receive params, so I need to pass them in URL for xls link_to generator.
My controller:
def show
#website = Website.find(params[:id])
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
#search_by_created_at
#performance_reports = #website.performance_reports.where("created_at between ? and ?", params[:report][:start_date].to_date, params[:report][:end_date].to_date)
else
#performance_reports = #website.performance_reports
end
respond_to do |format|
format.html # index.html.erb
format.xls
format.xml { render :xml => #performance_reports }
end
end
and my generated url looks like:
http://127.0.0.1:3000/websites/25/performance_reports/show?utf8=%E2%9C%93&report[end_date]=07%2F09%2F2012&report[start_date]=04%2F09%2F2012&commit=Run+Report
mine xls url is generated like this:
<%= link_to url_for(:format => 'xls') do%>
<%= image_tag("excel.png", :id => "analytics",:size => '21x23')%> <b>Export</b>
<% end %>
result:
http://127.0.0.1:3000/websites/25/performance_reports/show
Any help will be appreciated.
xls in not available by default.
Add this:
gem "spreadsheet"
gem "to_xls", :git => "https://github.com/dblock/to_xls.git", :branch => "to-xls-on-models"
Register the Excel MIME type in config/initializers/mime_types.rb by adding this:
Mime::Type.register "application/vnd.ms-excel", :xls
Add an as_xls method to model that you want to export for the fields you want.
For example for a User model you might have:
def as_xls(options = {})
{
"Id" => id.to_s,
"Name" => name,
"E-Mail" => email,
"Joined" => created_at,
"Last Signed In" => last_sign_in_at,
"Sign In Count" => sign_in_count
}
end
Add code to the controller:
def index
#users = User.all
respond_to do |format|
format.html
format.xls { send_data #users.to_xls, content_type: 'application/vnd.ms-excel', filename: 'users.xls' }
end
end
Provide a link:
= link_to 'Export', users_path(request.parameters.merge({:format => :xls}))
All code should have a test. You could do something like this:
describe "GET index.xls" do
it "creates an Excel spreadsheet with all users" do
user = Fabricate :user
get :index, :format => :xls
response.headers['Content-Type'].should == "application/vnd.ms-excel"
s = Spreadsheet.open(StringIO.new(response.body))
s.worksheets.count.should == 1
w = s.worksheet(0)
w.should_not be_nil
w.row(0)[0].should == "Id"
w.row(1)[0].should == user.id.to_s
w.row(0)[1].should == "Name"
w.row(1)[1].should == user.name
end
end
Related
controller.rb
def xml_print
byebug
#month = params[:month]
#year = params[:year]
#maps = SalaryMapSap.all
#salaryslip = Salaryslip.limit(50)
respond_to do |format|
format.xml { send_data render_to_string(:index), :filename => 'mydoc.xml', :type=>"application/xml", :disposition => 'attachment' }
# #salary_components = SalaryComponent.all
#maps = SalaryMapSap.all
end
end
routes.rb
resources :salaryslip_components do
collection do
post :xml_print
end
end
new.html.erb
<%= bootstrap_form_for(:salaryslip_component, url: { action: 'xml_print',format: 'xml',month: #month,year: #year}) do |f| %>
I want to generate an XML Data in rails,for that i have created one form,in that two drop downs are there where i would select month and year and on bottom there would be submit button,if i click on it then all my data would be downloaded as an XML.I created index XML builder file.But my data is not generating in XML ,error is coming,that action could not be found.I also mentioned this in controller's action.
format.xml { send_data render_to_string(:index), :filename => 'mydoc.xml', :type=>"application/xml", :disposition => 'attachment' }
So please help me.
How to redirect the two links to two urls by using single method in controller?
Example:
def index
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if # PDF EPC link clicked
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
if # LIVE EPC link clicked
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
end
in my view,
<%= link_to 'PDF', enr_location_current_epc_index_path(#location) %>
<%= link_to 'LIVE', enr_location_current_epc_index_path(#location) %>
in my routes
resources :current_epc, only: [:index, :show] do
get :download, :on => :collection
end
I would consider create 2 differente actions. One for each case. It would make your actions and your code much easier to read.
Then u would result with 3 actions. Index which would only load the initial objects, one to treat the specific id by the first logic and another link to treat the second logic.
def index
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
end
pdf_epc
#location_id = Location.find(#location_id)
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
def live_epc
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
in your routes
resources :current_epc, only: [:index, :show] do
get :download, :on => :collection
end
get "/pdf_epc/:id" => "current_epc#pdf_epc", :as => enr_location_current_epc_pdf
get "/live_epc/:id" => "current_epc#live_epc", :as => enr_location_current_live_epc
in your view
<%= link_to 'PDF', enr_location_current_epc_pdf_path(#location) %>
<%= link_to 'LIVE', enr_location_current_live_epc_path(#location) %>
def pdf_url
if (params[:url] == 1)
do something
else
do something else
end
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if #epc != nil
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end
end
In your routes.rb:
match "/anything/pdf_url/:url" => "anything#pdf_url"
And your two links:
<%= link_to "first", "/anything/pdf_url/1" %>
<%= link_to "second", "/anything/pdf_url/2" %>
EDIT:
member is used when it requires an :id parameter, if not it is a collection. Anyway, I would use match in that case like (which is in parenthesis is optional):
match "/anything(/download/:url)" => "anything#index"
and get the parameter in your controller like this:
def index
if params[:url] == 1 # Or whatever you put in your link_to
# redirect_to url
else
# redirect_to url
end
end
EDIT 2:
Index controller:
def index
if params[:id]
#location_id = Location.find(params[:id])
#epc = Enr::Rds::CurrentEpc.find_by_location_id(#location_id)
if params[:url] == 'pdf'
#epc.current_epc_path[-4..-1] == '.pdf'
content = open(#epc.current_epc_path, "rb") {|io| io.read }
send_data content, :filename => 'epc.pdf', :disposition => 'inline'
elsif params[:url] == 'live'
#epc = Enr::Rds::XmlData.find_by_location_id(#location_id)
redirect_to #epc.report_url
end
else
#locations = Location.all
respond_to do |format|
format.html
format.json { render :json => #locations }
end
end
end
Your routes:
match "/anything(/:id(/:url))" => "anything#index"
Your view (change links to fit your tastes, it's just a simple example):
<%= link_to "first", "/anything/1/pdf" %>
<%= link_to "second", "/anything/1/live" %>
I would like to create one CSV file containing two models with comma gem in my ruby 3.2.8 application. Maybe the answer to the question is trivial, but this is the first time I use this gem. I know how create the file it based on a model, but I don' t know how make matches of two.
I have a views\participants\index with :
<%= link_to 'Download CSV', '/participants.csv' %>
the controller :
def index
#participants = Participant.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #participants }
format.csv { send_data #participants.to_comma }
end
end
participant Model:
require 'comma'
class Participant < ActiveRecord::Base
comma do
id
token
end
end
and field Model:
require 'comma'
class Field < ActiveRecord::Base
comma do
name
value
id_participant
end
end
in the db i have:
Participant1 = ["id" => 1 , "token" => "a"]
Participant2 = ["id" => 2 , "token" => "b"]
Field1= ["id_participant" => 1, "name" => "p1_exams1", "value" =>5]
Field2= ["id_participant" => 1, "name" => "p1_exams2", "value" =>3]
Field3= ["id_participant" => 2, "name" => "p2_exams1", "value" =>2]
Field4= ["id_participant" => 2, "name" => "p2_exams2", "value" =>3]
I would like to have a file like this:
id token
1 a
id_p name value
1 p1_c1_exams1 5
1 p1_c1_exams2 3
id token
2 b
id_p name value
2 p1_c1_exams1 2
2 p1_c1_exams2 3
I tried with this in controller:
def index
#participants = Participant.all
#fields = Field.all
require 'csv'
csv_string = CSV.generate do |csv|
#participants.each do |p|
csv << ["id","token","last_ip_address","start_date","last_transition_date","completion_date","completed","total_time_survey","created_at"]
csv << [ p.id, p.token , p.last_ip_address, p.start_date, p.last_transition_date, p.completion_date, p.completed, p.total_time_survey, p.created_at]
#fields.each do |f|
if String(f.id_participant) == String(p.id)
csv << ["id","name","value","id_participant","id_survey","created_at"]
csv << [f.id,f.name, f.insert_value, f.id_participant, f.id_survey, f.created_at]
end
end
end
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #participants }
format.csv { send_data csv_string,
:type => "text/csv; charset=iso-8859-1; header=present",
:disposition => "attachment; filename=Database.csv" }
end
end
You can also use the fastercsv for this
i think this will help u what i am understanding that u have has many relationship between Participant and Field regarding this i have write some piece of code u can customize it as ur need
#participants = Participant.all
csv_string = FasterCSV.generate do |csv|
#participants.each do |i|
csv << ["id","token"]
csv << [ i.id, i.token ]
i.fields.each do |j|
csv << ["id_p","name", "value"]
csv << [i.id,j.name, j.value]
end
end
end
send_data csv_string,
:type => "text/csv; charset=iso-8859-1; header=present",
:disposition => "attachment; filename=anyName.csv"
I have the following in my tags controller (params[:q] comes from this plugin: http://loopj.com/jquery-tokeninput/). This is basically a slightly modified product of this screencast: http://railscasts.com/episodes/258-token-fields.
tags_controller.rb:
class TagsController < ApplicationController
def index
#tags = Tag.where("name like ?", "%#{params[:q]}%")
results = #tags.map(&:attributes)
results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
respond_to do |format|
format.html
format.json { render :json => results }
end
end
I want to only do results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"} only if the name doesn't exist already in #tags. Because right now, it looks like this:
programming #input field
programming #drop-down menu
Add: progamming #drop-down menu
I want it to just display like
programming #input field
Add: progamming #drop-down menu
How to accomplish that?
EDIT:
Here is the model and JavaScript just in case:
application.js
$(function() {
$("#post_tag_tokens").tokenInput("/tags.json", {
crossDomain: false,
prePopulate: $("#post_tag_tokens").data("pre"),
preventDuplicates: true,
theme: "facebook"
});
});
post.rb:
def tag_tokens=(ids)
ids.gsub!(/CREATE_(.+?)_END/) do
Tag.find_or_create_by_name(:name => $1).id
end
self.tag_ids = ids.split(",")
end
You can do this:
#tag = Tag.find_by_name(params[:q])
or
#tag = Tag.name_like(params[:q]) #For this you need to install gem [searchlogic][1]
if #tag.blank?
# Do you things
end
I have this controller:
class SchedulesController "post", :except => :index
def index
#Schedules.create(:id => -1, :deviceId => "2002", :subject => "Test 2", :scheduleTime => Time.new, :repeatEveryYear => "FALSE")
##schedules = Schedules.all
respond_to do |format|
format.html # list.html.erb
format.json { render :json => #schedules.to_json }
end
end
def create
#schedule = Schedules.new.from_json(params[:schedule])
#schedule.save
render :json => "success"
end
end
The Schedule has a dateTime field, how can I get the controller to return this time formatted as "yyyy-MM-dd HH:mm:zzzz" (zzzz = Specific GMT timezone)?
Thank you
Søren
You can specify the date and time formats in the initializers. For instance, create the file config/initializers/time.rb and put the following code:
Time::DATE_FORMATS[:schedule] = "%Y-%m-%d %H:%M:%z"
Then in your Schedule.rb:
def formatted_schedule_time
scheduleTime.to_s(:schedule)
end
And every time you call the to_json method on a Schedule object, you need to do:
#schedule.to_json(:methods => [:formatted_schedule_time])