Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
def get_xml
path = "ddd-66252.pdf" // in public
way = File.basename(path)
diff = File.read(path)
render :xml => diff
end
How i can get file from path and need to look like file
Assuming you are using Ruby on Rails because you mention a public folder and have a render method in your code. In Ruby on Rails you can use send_file in your controller like this to send files to the browser:
def send_pdf
send_file(
Rails.root.join('public', 'ddd-66252.pdf'),
filename: 'ddd-66252.pdf',
type: 'application/pdf'
)
end
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create an instance of a class that each includes an element from the class's array. The array is like this
class CorporatePanel < ActiveRecord::Base
TEXT_ONLY = 'text only'
IMAGE_LEFT = 'image left'
IMAGE_RIGHT = 'image right'
IMAGE_ONLY = 'image only'
VIDEO = 'video'
PANEL_TYPE = [TEXT_ONLY, IMAGE_LEFT, IMAGE_RIGHT, IMAGE_ONLY, VIDEO]
So for each panel type i want an instance of the class corporate panel.
#corp_page = CorporatePage.create!(title: 'Home', static_descriptor: 'Home', workflow_state: 'draft')
#corp_panel = CorporatePanel::PANEL_TYPE
if #corp_page.title == 'Home'
puts "PAGE CREATED"
(#corp_panel.count).times do
corp_panel = CorporatePanel.new
#corp_panel.each do |x|
corp_panel.panel_type = x
end
if corp_panel.save
puts "#{corp_panel.title} created"
else
puts "#{corp_panel.title} not created"
puts corp_panel.errors.inspect
end
end
else
puts "Corp Page not saved"
puts #corp_page.inspect
end
So on every iteration through corporate panel it uses a different panel type. At the moment it creates five of each panel!
Thanks in Advance folks
Looking at the array documentation i found the method 'pop', to remove the last element of every array, seeing as this constant is an array, it worked.
corp_panel.panel_type = CorporatePanel::PANEL_TYPE.pop
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have next method:
def self.authorize(auth_data)
provider = Provider.find_or_create_by(name: auth_data['name'], uid: auth_data['uid'])
if provider.user.present?
provider.user
else
user = User.find_or_create_with_oauth(auth_data['info'])
provider.update(user: user)
user
end
end
And I can replace if-else condition with if-return:
def self.authorize(auth_data)
provider = Provider.find_or_create_by(name: auth_data['name'], uid: auth_data['uid'])
return provider.user if provider.user.present?
user = User.find_or_create_with_oauth(auth_data['info'])
provider.update(user: user)
user
end
I don't know what is more ruby style, or preferably in general.
You could also consider:
def self.authorize(auth_data)
provider = Provider.find_or_create_by(name: auth_data['name'], uid: auth_data['uid']
provider.update(user: User.find_or_create_with_oauth(auth_data['info'])) unless provider.user.present?
provider.user
end
Alternatively, you might find this more readable:
def self.authorize(auth_data)
provider = Provider.find_or_create_by(name: auth_data['name'], uid: auth_data['uid'])
unless provider.user.present?
user = User.find_or_create_with_oauth(auth_data['info'])
provider.update(user: user)
end
provider.user
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the URL http://localhost:3000/en/invest_offers?utf8=blahblahblah. How can I get ?utf8=blahblahblah part of the URL? Thanks!
Do as below using URI::Generic.query:
require 'uri'
URI("http://localhost:3000/en/invest_offers?utf8=blahblahblah").query
# => "utf8=blahblahblah"
Use URI::parse, URI::Generic#query:
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
URI.parse(url)
# => #<URI::HTTP:0x0000000213aae0 URL:http://localhost:3000/en/invest_offers?utf8=blahblahblah>
URI.parse(url).query
# => "utf8=blahblahblah"
Do the following:--
require 'uri'
url = 'http://localhost:3000/en/invest_offers?utf8=blahblahblah'
parsed_url = URI.parse(url)
if you just want to get string query part like "utf8=blahblahblah"
you should do
parsed_url.query
if you want get "blahblahblah",
you should just do
params["utf8"]
or else
p = CGI.parse(parsed_url.query)
# p is now {"utf8"=>["blahblahblah"]}
p["utf8"].first
#=> "blahblahblah"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm struggling with the following code:
I want a method to check if a string has content or not.
has_content = -> (a) { a!=nil && a.strip != ''}
c = ' '
has_content.call(c)
=> false
c.has_content
=> true
Why is the response different? Clearly I am lacking some Proc/lambdas knowledge.
I believe there is something missing in that code that is causing such behavior.
has_content is not defined for String, so unless you defined it before, it should raise an error
1.9.3p429 :002 > ''.has_content
NoMethodError: undefined method `has_content' for "":String
from (irb):2
from /Users/weppos/.rvm/rubies/ruby-1.9.3-p429/bin/irb:12:in `<main>'
As a side note, here's an alternative version of your code
has_content = ->(a) { !a.to_s.strip.empty? }
And here's an example
has_content.(nil)
# => false
has_content.('')
# => false
has_content.(' ')
# => false
has_content.('hello')
# => true
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to read an XML file and store the structure into an array of objects. Here is my code:
class Bike
attr_accessor :id, :color, :name
def initialize(id, color, name)
#id = id
#color = color
#name = name
end
end
---x---snip---x---
rules.root.each_element do |node1|
case node1.name
when "Bike"
bike = Bike.new(node1.attributes['id'], node1.attributes['color'], { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name"})
bikes.push bike
end
end
However, the last element is not fetching the value alone. It is fetching the whole tag. Is there a better way to do it?
Your code block { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name" }
doesn't seem to make sense inside the new parameter list.
Where does bike_elem come from? This is not valid Ruby in this context.
It's hard to give an answer here without knowing what your XML looks like.
But I would recommend using a XML library like Nokogiri, libxml, and then parse out
the name before you do the new. Try using XPath.
rules.root.each_element do |node1|
case node1.name
when "Bike"
name = node1.attributes[....]
bike = Bike.new(node1.attributes['id'], node1.attributes['color'],name )
bikes.push bike
end
end