I'am new to ruby on rails. I already import all the countries in csv file. Now, I want to add my own design images for each country. What is the easiest way to add the images for each country? is it I need use model or controller to add it? Thanks
I am assuming that you have a model call country.rb and it looks like
create_table "countries", force: :cascade do |t|
t.string "name", default: "", null: false
t.string "image", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
you can now use a gem called 'carrierwave'
then run
rails generate uploader Image
in your model
class Country < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
I hope this was helpful
Related
I have a Rails 5 app in which I use Globalize for localization. I'm currently having an issue where I can't save new objects if they don't have any translations.
My model looks like this:
# Product.rb
translates :description, :fallbacks_for_empty_translations => true
has_many :translations
accepts_nested_attributes_for :translations
# ProductTranslation.rb
belongs_to :product, optional: true
My database schema looks for my product_translations looks like this:
t.integer "product_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.index ["locale"], name: "index_product_translations_on_locale"
t.index ["product_id"], name: "index_product_translations_on_product_id"
When I try to save new products I currently get this error:
Unable to save product with id = got these validation errors {:"translations.globalized_model"=>["translation missing: sv.activerecord.errors.models.product/translation.attributes.globalized_model.required"]}
Any ideas on what I can do to make it work?
I have a very simple model that each event has many forexes. I am trying to create a nested form to create new event with a bunch of forexes in a go.
class Event < ActiveRecord::Base
has_many :forexes
accepts_nested_attributes_for :forexes
end
class Forex < ActiveRecord::Base
belongs_to :event
end
The schema is like this:
ActiveRecord::Schema.define(version: 20180505093823) do
create_table "events", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "base"
end
create_table "forexes", force: :cascade do |t|
t.string "code"
t.float "rate"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "forexes", ["event_id"], name: "index_forexes_on_event_id"
end
And then I tried to create new objects using the following code in rails console. It fails.
Event.new( name: "11", base: "HKD", forexes_attributes: [ {code: "RMB", rate:1}, {code: "CNY",rate:2}])
It throws me back with this error.
ActiveRecord::UnknownAttributeError: unknown attribute 'forexes_attributes' for Event.
I know this is quite a basic question. And I have tried many different ways after researching in different places. I couldn't debug it. Appreciate your help.
In your Event controller you need to include the forexes_attributes in event_params method as along with default one
def event_params
params.require(:event).permit(forexes_attributes: Forexes_attribute_names.map(&:to_sym).push(:_destroy))
end
I have a table that is part of a mountable blog engine in Rails. That table is called lines_articles.
def change
create_table "lines_articles", force: true do |t|
t.string "title"
t.string "sub_title"
t.text "content"
t.boolean "published", default: false
t.datetime "published_at"
t.string "hero_image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.string "gplus_url"
t.boolean "featured", default: false
t.string "document"
t.string "short_hero_image"
end
I want to show all contents in that table in my rails console, so I ran a .classify to make sure I didn't screw up the plural variable, and the output was:
2.2.1 :015 > "lines_articles".classify
=> "LinesArticle"
So now the console confirmed the naming convention, but when I run a simple LinesArticle.all, I get an error: NameError: uninitialized constant LinesArticle.
How can the console classify a table name and throw a NameError when querying the table with that name?
You have to define a model class in the app/models/lines_article.rb directory.
If you're on Rails 5, it should look something like this:
class LinesArticle < ApplicationRecord
end
If you're on Rails 4, it should look something like this:
class LinesArticle < ActiveRecord::Base
end
Then re-open rails console and try again.
I've got a data model represented by the following image:
Basically, the idea is that there are many different Collections. All the Items in a single collection will have the same Attributes, but the list of attributes will be different per collection. The value of each Attribute per item will be stored in Item Attribute Values.
I'm trying to build a single page where a user can populate the attributes for an item. I'm assuming a nested form is the way to go but I'm at a loss as to how to represent this in the controller and on the page, considering the names of the attributes are in one table and the values in another.
If anyone has encountered or had to deal with a similar situation, any help would be appreciated.
Thanks
Here is one potential solution.
class Collection
has_and_belongs_to_many :items
has_and_belongs_to_many :attributes
end
class Item
has_and_belongs_to_many :collections
has_many :item_attributes
has_many :attributes, though: :item_attributes
end
class Attributes
has_and_belongs_to_many :collections
has_many :item_attributes
has_many :items, though: :item_attributes
end
class ItemAttribute
belongs_to :item
belongs_to :attribute
end
So lets look at the database layout to back these models:
ActiveRecord::Schema.define(version: 20151027173337) do
create_table "attributes_collections", id: false, force: :cascade do |t|
t.integer "attribute_id", null: false
t.integer "collection_id", null: false
end
create_table "collections", force: :cascade do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "collections", ["user_id"], name: "index_collections_on_user_id"
create_table "collections_items", id: false, force: :cascade do |t|
t.integer "collection_id", null: false
t.integer "item_id", null: false
end
create_table "item_attributes", force: :cascade do |t|
t.integer "item_id"
t.integer "attribute_id"
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "item_attributes", ["attribute_id"], name: "index_item_attributes_on_attribute_id"
add_index "item_attributes", ["item_id"], name: "index_item_attributes_on_item_id"
create_table "items", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
A Better way?
But of course performance will suffer due to the many joins. Plus each attribute will be stored as a VARCHAR which means you can't do any numeric comparisons in the database.
If you really need a flexible schema i would instead look into using HSTORE, JSON or another dynamic column type or a schemaless database such as MongoDB.
How do I create a form / controller for this?
First you should get very acquainted with what can be done with accepts_nested_attributes_for and fields_for and maybe consider using AJAX to delegate the actions out to CollectionController and a ItemController on the back end rather than cramming it all into a single monstrosity.
I have a Movie model and an Actor model:
class Movie < ActiveRecord::Base
belongs_to :genre
has_many :reviews
has_many :actors
end
class Actor < ActiveRecord::Base
belongs_to :movies
end
These are the attributes for each model:
create_table "actors", force: :cascade do |t|
t.string "name"
t.text "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "movie_id"
end
create_table "movies", force: :cascade do |t|
t.string "title"
t.integer "duration"
t.date "release_date"
t.text "plot"
t.string "director"
t.text "cast"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
When a user fills out the form to create a new movie, I want the input from the 'cast' field to save to the Actor model. What action(s) would I need in my controller and what would I need to do in my form?
I've looked at and tried the following and I'm still stuck:
Rails Updating Data in one Model from another Model's Controller
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
How can I update and save one model from another in Rails?
http://railscasts.com/episodes/196-nested-model-form-part-1
Any help is greatly appreciated, thanks!
You want to create a nested form. You'll need to add accepts_nested_attributes_for :actors to the movie model, then build a subform within the form... Better explanation here:
http://www.theodinproject.com/ruby-on-rails/advanced-forms
Scroll down to "Nested Forms".