I have SomeController
and have #xyz object
#xyz = XYZ.new(params[:xyz])
and I have to save all fields from form to xml file.
So what are the steps.
Please help me.
I think following should work
#xyz = XYZ.new(params[:xyz])
xml_string = #xyz.to_xml
File.open("some_file_name.xml", 'w') { |f| f.write(xml_string) }
Related
I am new to rails. I want to generate a pdf so for that I have used pdfkit gem. I followed the railcast tutorial but the problem in my case is that first I am searching for the record between the two dates than it redirects to a page and I have to print that page into pdf. The page that i have to generate pdf have some parameters in the address bar like this : http://localhost:3000/report?utf8=%E2%9C%93&mer_report%5Bmerchant_id%5D=32&sdate=2015-06-13&edate=2015-10-13
Help me how i will do that.
View :
<p><%= link_to "Download", report_path %></p>
Controller :
merchant_id = params[:mer_report][:merchant_id].to_i
merchant = Merchant.find(merchant_id)
$merchant = merchant
#merchant_id = merchant.id
#start_date = params[:sdate]
#end_date = params[:edate]
#original_merchant_name = merchant.name
#total_customer = merchant.users
#total_visits = merchant.visits.where(checked_in: true)
application.rb :
config.middleware.use "PDFKit::Middleware", :print_media_type => true
I have extended RefineryCMS blog/admin/posts controller with Export action, which export post to xml and save it to file. I need export all translated versions of post. When I click export it saves all files, but all files have same locale.
Is it anything wrong with my code:
def generate_xml_by_locales
translated_locales.each do |l|
::I18n.locale = l
file = File.new("blog_#{l}.xml", "wb")
xml = Builder::XmlMarkup.new :target => file
xml.instruct!
xml.blog do
xml.title post.title
xml.description "Sixteenth installment of development diary"
xml.language l.to_s
xml.author "Dan"
xml.date post.created_at
xml.category "diary"
xml.text post.body
end
file.close
end
end
Thanks for help.
I did one modification, which work just fine. Instead of changing locale via I18n.locale I use translations.where(locale: lang).first . Don't know if it is the best solution, but it just works.
Refactored code:
def generate_xml_by_locales
translated_locales.each do |lang|
generate_xml(translations.where(locale: lang).first, lang)
end
end
def generate_xml post, lang
file = File.new("blog_#{lang}.xml", "wb")
xml = Builder::XmlMarkup.new :target => file
xml.instruct!
xml.blog do
xml.title post.title
xml.description "Sixteenth installment of development diary"
xml.language lang.to_s
xml.author "Dan"
xml.date post.created_at
xml.category "diary"
xml.text post.body
end
file.close
end
I have the following code in my controller
#current_chat = current_user.sent_messages
.where("created_at > ? and receiver_id = ?", current_user.current_sign_in_at, current_chat[:receiver_id].to_i)
.select("body, created_at").each { |message| message.instance_eval { def type; #type end; #type = 'sent' } }
And I'm passing the #current_chat object to a partial like so:
<%= render partial: 'shared/chat_form', locals: { messages: #current_chat } %>
But I'm getting the following error:
singleton can't be dumped
At the first line in ActiveSupport::MessageVerifier#generate
def generate(value)
data = ::Base64.strict_encode64(#serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end
Any ideas on how to fix this?. Thanks in advance.
You can not use this
#serializer.dump(value)
This is causing the error. Read this link, its all about using singletons in ruby.
link
Would it be possible to feed a single.jpg true carrierwave?
Using jpegcam Im generating a temp.jpg and would like to feed this in carrierwave
so it gets stored in the photos table and generate the thumbnails based on the /uploaders/photo_uploader.rb
Any way to feed a single jpg to carrierwave?
def upload
File.open(upload_path, 'w:ASCII-8BIT') do |f|
f.write request.raw_post
end
render :text => "ok"
end
private
def upload_path # is used in upload and create
file_name = ("webcam_1.jpg")
File.join(::Rails.root.to_s, 'public', 'uploads', file_name)
Photo.create(:file => File.open("#{Rails.root}/public/uploads/#{file_name}"))
end
If I understand your question correctly, you just want to create a Photo from a file? Assuming your Photo class has an 'image' field that Carrierwave is using, that would be this:
Photo.create(:image => File.open("#{Rails.root}/public/uploads/#{file_name}"))
Hi Im trying to parse XML from a websites API with Nokogiri. Im just curious to see if Im on the right track. I have a controller wich handles the parsing and then I would like the model to initialize the necessary parameters and then display it as a simple list in the view. I was thinking something like this in the Controller:
def index
doc = Nokogiri::XML(open("http://www.mysomething.com/partner/api/1_0/somerandomkeynumber4b0/channel/11number/material/list/").read)
#news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
#news << News.new(
:header => header,)
end
and then the Model:
class News
include ActiveModel::Validations
validates_presence_of :url, :type_of_media
attr_accessor :header, :source_name, :summary, :url, :created_at, :type_of_media
def initialize(attributes = {})
#header = attributes[:header]
#source_name = attributes[:source_name]
#summary = attributes[:summary]
#url = attributes[:url]
#created_at = attributes[:created_at]
#type_of_media = attributes[:type_of_media]
end
Is this how you would do this?! Not sure Im thinking correct on this. Maybe you have any tips on a great way of incorporating Nokogiri with some other thing for the view like Google maps or something. Right now Im getting an error saying
Missing template news/index with {:formats=>[:html], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml], :locale=>[:en, :en]} in view paths
Thanks in advance!
#noodle: So this:
#news = doc.css('query').map do |n|
h = {}
%w(header source_name summary url created_at type_of_media).each do |key|
h[key.to_sym] = n.css(key).text
end
News.new(h)
end
Is equal to:
#news = []
doc.css("news").each do |n|
header = n.css("header").text
source_name = n.css("source_name").text
summary = n.css("summary").text
url = i.css("url").text
created_at = i.css("created_at").text
type_of_media = i.css("type_of_media").text
#news << News.new(
:header => header,)
end
Did I understand you correctly?! Regarding the template I have located the the problem. It was a minor misspelling. Cheers!
You're really asking two questions here..
Is my xml -> parse -> populate pipeline ok?
Yes, pretty much. As there's no conditional logic in your .each block it would be cleaner to do it like this:
#news = doc.css('query').map do |n|
#...
News.new(:blah => blah, ...)
end
.. but that's a minor point.
EDIT
You could save some typing by initializing a hash from the parsed xml and then passing that to Model.new, like:
#news = doc.css('query').map do |n|
h = {}
h[:header] = n.css('header').text
# ...
News.new(h)
end
EDIT 2
Or even shorter..
#news = doc.css('query').map do |n|
h = {}
%w(header source_name summary url created_at type_of_media).each do |key|
h[key.to_sym] = n.css(key).text
end
News.new(h)
end
In fact #inject could make that shorter still but I think that'd be a little obfuscated.
Why can't rails find my view template?
Dunno, is there one? You've not given enough details to answer that part.