Rails 4 - Seed images from hard drive with carrierwave - ruby-on-rails

I'm trying to seed images with carrierwave. After looking at some questions, including this one, I have tried:
c1 = Category.create(
name: "About Us",
page_title: "12 Years of Tradition",
page_description: "long description here",
page_content: "a lot of content here",
link_to_subcategory: "Meet the Robinson's",
banner_image: open("RF_web_about_us_1.jpg"),
main_image: open("RF_web_about_us_2.jpg"),
home_page_content: "content here"
)
Unfortunately, I get the following error when I run rake db:seed
rake aborted!
Errno::ENOENT: No such file or directory - RF_web_about_us_1.jpg
The image (RF_web_about_us_1.jpg) is stored in app/assets/images. Any help at all with this would be much appreciated.

You have to specify the full path to the image:
File.open(Rails.root + "app/assets/images/RF_web_about_us_1.jpg")

Another way to do this:
Rails.root.join("app/assets/images/RF_web_about_us_1.jpg").open
So your create statement will look like:
c1 = Category.create(
name: "About Us",
page_title: "12 Years of Tradition",
page_description: "long description here",
page_content: "a lot of content here",
link_to_subcategory: "Meet the Robinson's",
banner_image: Rails.root.join("app/assets/images/RF_web_about_us_1.jpg").open,
main_image: Rails.root.join("app/assets/images/RF_web_about_us_2.jpg").open,
home_page_content: "content here"
)
Reference: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-%22Upload%22-from-a-local-file

Related

Trouble Extracting Data with Nokogiri

I'm practicing extracting data from an XML site and I'm using Nokogiri to read and parse. I need to analyze the data but for now, I'm just trying to get an output with no success.
I have the following code:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml"))
doc.xpath('//PERSONA').each do |char_element|
puts char_element.text
end
I'm simply trying to read the characters off the XML website, but I'm not getting any results when I run it in the terminal. I also tried just writing a simple xpath call such as the one below:
doc.xpath("//PERSONA")
or
doc.xpath("PLAY TITLE")
And I get either an error or it simply acts as if nothing was entered.
I have put a simple function to test it so I know it's reading it. Can anyone tell me what I'm doing wrong?
You're trying to read XML file as a HTML one.
Please try that example:
doc = Nokogiri::XML(open("http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml"))
doc.xpath('//PERSONA').each{|ce| p ce.text }
"DUNCAN, king of Scotland."
"MALCOLM"
"DONALBAIN"
"MACBETH"
"BANQUO"
"MACDUFF"
"LENNOX"
"ROSS"
"MENTEITH"
"ANGUS"
"CAITHNESS"
"FLEANCE, son to Banquo."
"SIWARD, Earl of Northumberland, general of the English forces."
"YOUNG SIWARD, his son."
"SEYTON, an officer attending on Macbeth."
"Boy, son to Macduff. "
"An English Doctor. "
"A Scotch Doctor. "
"A Soldier."
"A Porter."
"An Old Man."
"LADY MACBETH"
"LADY MACDUFF"
"Gentlewoman attending on Lady Macbeth. "
"HECATE"
"Three Witches."
"Apparitions."
"Lords, Gentlemen, Officers, Soldiers, Murderers, Attendants, and Messengers. "
Please be sure you're using Nokogiri::XML instead of Nokogiri::HTML

Inline Attachment image is not displaying in Rails 4

I've been trying to show inline attachments in my mail (logo),
I've seen many answers on the community, nothing seems to be working for me,
Mail is going fine,I can see the text but unable to see image
this is my mailer:
path variable contains path to logo
attachments.inline['logo.png'] = { content: File.read(path),mime_type: "image/png" }
this is my view:
Welcome to my site
<%= image_tag attachments['logo.png'].url%>
Please Help,I've been pulling my hair for hours now on this.
In your mailer try:
attachments.inline["myimage.png"] = File.read(File.join(Rails.root, "app", "assets", "images", "myimage.png"))
your view code is correct.
I've been using this code successfully with one image, though there may be a bug in rails. Check: https://github.com/rails/rails/issues/2686

How to translate errors and attributes of model in Rails?

I've changed default locale of my project, and now there is the following error:
Average check translation missing: ru.activemodel.errors.models.place_restaurant.attributes.average_check.blank
Average check is ':average_check' in 'PlaceRestaurant' model. I want to change 'Average check' text for russian analogue, and add text of error. I've made 'ru.yml' file:
ru:
activerecord:
models:
place_restaurant: "Some russian analogue"
attributes:
place_restaurant:
average_check: "Some russian analogue"
But it doesn't work, average_check is still 'Average check'. How can I fix it?
An error message gives you all structure how it should looks like. Yaml should be:
ru:
activemodel:
errors:
models:
place_restaurant:
attributes:
average_check:
blank: "Не может быть пустым"
In general this error message seems to be strange for me. Are you using default presence validation?
This is how a Locale file should e like. Yo have not mentioned the "errors".
https://gist.github.com/satyatechsavy/7671560

Middleman Localization gives 404

I'm trying to user MiddlemanApp to localize a website, following this http://middlemanapp.com/guides/localization guide.
I have a locales/ folder under the project's root, with en.yml and it.yml inside. The files looks like this:
en.yml
---
en:
rooms: "rooms"
restaurant: "restaurant"
bar: "bar"
contacts: "contacts"
gallery: "gallery"
about: "about the website"
press: "press"
it.yml
---
it:
rooms: "stanze"
restaurant: "ristorante"
bar: "bar"
contacts: "contatti"
gallery: "galleria"
about: "il sito web"
press: "stampa"
The localization files seem to be loaded, when i start the server i get:
Using inline Guardfile.
Guard is now watching at '...'
LiveReload 1.6 is waiting for a browser to connect.
== Locales: en, it
== The Middleman is standing watch on port 4567
when i navigate the the site root http://localhost:4567/ the page loads and the localization strings are taken from the it.yml localizazion. Shouldn't it go to the en.yml? According to the documentation it should take the locales in alphabetic order if not differently specified.
When i navigate to http://localhost:4567/en/index.html or http://localhost:4567/it/index.html, instead, i get a:
File Not Found
/it/index.html
I tried to restart the server, change the parameters of the localize function in config.rb (path and mount_to_root), but got nothing different. Anyone have ideas?
This will depend on the contents of your /source/localizable folder. Only templates in this folder will have access to i18n values.
If you're still having issues, please submit a bug report.

Is there any limitation on YAML indentation level?

I am developing a Ruby on Rails application with I18n support. I created a YAML file. But Rails report there is syntax error in the YAML file.
I found that if I decrease the level of indentation for that error line, no error message come out again.
Is there any limitation on YAML indentation level (in Ruby on Rails)?
Here is YAML block. The line fulltime: "Full Time" was reported has syntax error.
en:
jobs:
new:
positiontitle: "Position Title"
country: "Country"
city: "City"
employmenttype: "Employment Type"
fulltime: "Full Time"
parttime: "Part Time"
Thanks everyone. :)
there's no limitation. possible cause of error is using a TAB characters instead of spaces when indenting YAML file lines
also your yaml file indentation is meaningless - if you want to indent fulltime & parttime - then you have to remove "Employment Type" string, so:
en:
jobs:
new:
positiontitle: "Position Title"
country: "Country"
city: "City"
employmenttype:
fulltime: "Full Time"
parttime: "Part Time"

Resources