Image upload using carrierwave in show page - ruby-on-rails

Using Rails 4 and Ruby 2.2,
I have book as model which should have image upload functionality in the show page of book. So User can create the book first and from show page upload the multiple images for book. I have used carrierwave as gem and have separate model created for Image.
image.rb
class Image < ActiveRecord::Base
belongs_to :book
mount_uploader :avatar, AvatarUploader
end
book.rb
class Book < ActiveRecord::Base
belongs_to :subject, dependent: :destroy
belongs_to :user, dependent: :destroy
has_many :images, dependent: :destroy
end
books/show.html.erb
<%= form_for(Image.new, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<%= hidden_field_tag "image[book_id]", #book.id %>
</div>
<%= f.fields_for :images do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "images[avatar]" %>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
schema.rb
create_table "images", force: :cascade do |t|
t.datetime "avatar_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "book_id", limit: 4
t.string "avatar", limit: 255
t.string "name", limit: 255
end
create_table "books", force: :cascade do |t|
t.string "title", limit: 255
t.integer "page_number", limit: 4
t.text "description", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id", limit: 4
t.integer "subject_id", limit: 4
t.boolean "active", default: false
end
Now I am kind of unable to proceed with this, can someone guide me on this because I am having form in books/show page and I need to show the image on the same page after successful updation of images.(Multiple images can be uploaded)
Thanks in advance
let me know if I need to provide any more information.

You can use the association itself and list all the images which have been uploaded.
<%= form_for(Image.new, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<%= hidden_field_tag "image[book_id]", #book.id %>
</div>
<%= f.fields_for :images do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "images[avatar]" %>
</div>
<% end %>
<!-- Listing all the images for a book -->
<% if #book.images.any? %>
<% #book.images.each do |img| %>
<%= image_tag(img.avatar.url) %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Related

Rails 5: Many-to-Many Relationship Not Working

I am trying to create a many-to-many table relationship in a Rails 5 project.
One Shop can have many Categories, which are indexed in the Shop_Categories table.
I seem to be missing one key step though. When I submit my Shop form, I receive the soft error: "Unpermitted parameter: :shop_category"
I can see my shop_category parameters being successfully sent in the Rails server log, but the Shop_Categories table doesn't get updated at all.
What am I missing, and what can I change so that pressing save on the Shop view form updates the Shop_Categories table?
Here's my code.
Shop Model
class Shop < ApplicationRecord
belongs_to :type
has_many :shop_categories
has_many :categories, through: :shop_categories
accepts_nested_attributes_for :shop_categories
enum status: [:open, :closed, :opening_soon, :closing_soon]
end
Shop_Category Model
class ShopCategory < ApplicationRecord
belongs_to :shop
belongs_to :category
end
Category Model
class Category < ApplicationRecord
has_many :subcategories, dependent: :destroy
has_many :shop_categories
has_many :shops, through: :shop_categories
end
Shop Controller (Excerpt)
def new
#shop = Shop.new
#shop.shop_categories.build
end
def create
#shop = Shop.new(shop_params)
#shop.open_date = params[:shop][:open_date]+"-01"
#shop.close_date = params[:shop][:close_date]+"-01"
if #shop.save
redirect_to #shop
else
render 'new'
end
end
def update
#shop = Shop.find(params[:id])
if #shop.update(shop_params)
redirect_to #shop
else
render 'edit'
end
end
private
def shop_params
params[:shop][:open_date] = params[:shop][:open_date]+"-01"
params[:shop][:close_date] = params[:shop][:close_date]+"-01"
params.require(:shop).permit(:shop_name, :type_id, :status,
:phone_number, :mobile_number, :email_address, :open_date,
:close_date, shop_categories_attributes: [ :shop_id, :category_id] )
end
Shop Form View (Excerpt)
<p>
<%= form.label :shop_name %><br>
<%= form.text_field :shop_name %>
</p>
<%= form.fields_for :shop_category do |category_fields| %>
<p>
<%= category_fields.label :category %><br />
<%= category_fields.collection_select(:category_id, Category.all, :id, :category_name, include_blank: true) %>
</p>
<% end %>
<p>
<%= form.submit %>
</p>
And finally, the Database Schema
create_table "categories", force: :cascade do |t|
t.string "category_name"
t.text "category_description"
t.string "visible_category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shop_categories", force: :cascade do |t|
t.string "visible_category_override"
t.integer "shop_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_shop_categories_on_category_id"
t.index ["shop_id"], name: "index_shop_categories_on_shop_id"
end
create_table "shops", force: :cascade do |t|
t.string "shop_name"
t.integer "status"
t.integer "sale"
t.string "phone_number"
t.string "mobile_number"
t.string "email_address"
t.date "open_date"
t.date "close_date"
t.integer "type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["type_id"], name: "index_shops_on_type_id"
end
Any suggestions would be greatly appreciated. I just can't work out which piece of the puzzle I'm missing!
Update: Including the Dev log, and the full Shop Form.
Dev Log
Started PATCH "/shops/1" for 127.0.0.1 at 2018-01-14 11:08:39 +0000
Processing by ShopsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2qydDj9JovMrg9VT6Lo5xbcNSHl5MH0ylmq3IMMX4iRTAVSgK6JlXWpG+CyuwQK6svZJn9wtnEnZANUlOZYzXA==",
"shop"=>{"shop_name"=>"test", "type_id"=>"1",
"shop_categories"=>{"category_id"=>"1"}, "subcategory_id"=>"",
"status"=>"open", "phone_number"=>"123", "mobile_number"=>"13212",
"email_address"=>"hello#test.com", "open_date"=>"2017-12",
"close_date"=>"2017-12"}, "commit"=>"Update Shop", "id"=>"1"}
Shop Load (0.5ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Unpermitted parameters: :shop_categories, :subcategory_id
(0.1ms) begin transaction
Type Load (0.2ms) SELECT "types".* FROM "types" WHERE "types"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.1ms) commit transaction
Redirected to http://localhost:3000/shops/1
Completed 302 Found in 10ms (ActiveRecord: 0.9ms)
Full view/shops/_form.html.erb file
<%= form_with model: #shop, local: true do |form| %>
<% if #shop.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#shop.errors.count, "error") %> prohibited
this shop from being saved:
</h2>
<ul>
<% #shop.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :shop_name %><br>
<%= form.text_field :shop_name %>
</p>
<p>
<%= form.label :type %><br />
<%= form.collection_select(:type_id, Type.all, :id, :type_name) %>
</p>
<%= form.fields_for :shop_categories do |category_fields| %>
<p>
<%= category_fields.label :category %><br />
<%= category_fields.collection_select(:category_id, Category.all, :id, :category_name, include_blank: true) %>
</p>
<% end %>
<p>
<%= form.label :subcategory %><br />
<%= form.grouped_collection_select :subcategory_id, Category.all, :subcategories, :category_name, :id, :visible_subcategory, include_blank: true %>
</p>
<p>
<%= form.label :status %><br />
<%= form.select :status, Shop.statuses.keys %>
</p>
<p>
<%= form.label :phone_number %><br>
<%= form.telephone_field :phone_number %>
</p>
<p>
<%= form.label :mobile_number %><br>
<%= form.telephone_field :mobile_number %>
</p>
<p>
<%= form.label :email_address %><br>
<%= form.email_field :email_address %>
</p>
<p>
<%= form.label :open_date %><br>
<%= form.month_field :open_date %>
</p>
<p>
<%= form.label :close_date %><br>
<%= form.month_field :close_date %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
It should probably be :shop_categories, not :shop_category
<%= f.fields_for :shop_categories do |category_f| %>
...
<% end %>
You also kind of lost me in the Shop_Category Model part. The actual class name is different (ShopSubcategory) and it belongs_to a subcategory? This will probably cause some errors as well. I guess what you meant is the ShopCategoryjoin model and it belongs_to :category
Update
Also be sure to build your nested resource when creating a new form:
#shop_controller
def new
#shop = Shop.new
#shop.shop_categories.build
...
end

Rails many to many model with select in the view

The logic in the exercise is create a Bill where a Bill have a list of Products with an amount of them.
I have the next 3 models:
Bill
Product
BillItem (This is the intermediate model between Bill and Product for the many to many relationship)
Schema for more clarity:
create_table "bill_items", force: :cascade do |t|
t.integer "amount"
t.integer "product_id"
t.integer "bill_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["bill_id"], name: "index_bill_items_on_bill_id"
t.index ["product_id"], name: "index_bill_items_on_product_id"
end
create_table "bills", force: :cascade do |t|
t.string "user_name"
t.string "dni"
t.date "expiration"
t.float "sub_total"
t.float "grand_total"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "description"
t.float "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
First i have a list of created products in my database.
For my BillController i have a _form.html.erb where i want be capable to create dynamic selects for choose a product and set a amount.
My view is the next:
<%= form_for bill do |f| %>
<div>
<%= f.label :user_name %>
<%= f.text_field :user_name %>
</div>
<div>
<%= f.label :dni %>
<%= f.text_field :dni %>
</div>
<div>
<%= f.label :product %>
<%= f.collection_select :product_ids, Product.all, :id, :name %>
</div>
# Here i should add more selects if the user want to add more products.
<div><%= f.submit %></div>
<% end %>
My problem or my question is: how to "groupe" the ids of the products and the amount of them. And the another question is, i can create the other selects dynamically with JS?
You need to be adding multiple BillItem to your form to account for the amount and the product. This is done through accepts_nested_attributes_for on the model. For example:
class Bill < ActiveRecord::Base
has_many :bill_items
accepts_nested_attributes_for :bill_items
end
Initialize a BillItem on the Bill in your controller:
class BillsController < ActionController::Base
def new
#bill = Bill.new
#bill.bill_items.build
end
end
Then in your form, use the fields_for helper to create sub-forms:
<%= form_for #bill do |f| %>
<div>
<%= f.label :user_name %>
<%= f.text_field :user_name %>
</div>
<div>
<%= f.label :dni %>
<%= f.text_field :dni %>
</div>
<%= f.fields_for :bill_items do |f| %>
<div>
<%= f.label :product %>
<%= f.collection_select :product_id, Product.all, :id, :name %>
</div>
<div>
<%= f.label :amount %>
<%= f.number_field :amount %>
</div>
<% end %>
<%= f.submit %></div>
<% end %>
And yes, you can use javascript to create new nested forms.

Creating Rails objects using same form

I am building an application to manage debts and am trying to create three objects using the same form.
The models are;
Debt (the amount owed)
class Debt < ActiveRecord::Base
belongs_to :user
belongs_to :creditor
belongs_to :debtor
accepts_nested_attributes_for :debtor, :creditor
end
Debtor (The person who owes)
class Debtor < ActiveRecord::Base
belongs_to :user
has_many :debts
end
Creditor (The person who is owed)
class Creditor < ActiveRecord::Base
belongs_to :user
has_many :debts
end
I am using the new debt form and would like to show fields for debtor and creditor. So when a new debt is created, so is the associated debtor and creditor.
I have viewed the documentation however, cannot get the fields for debtor or creditor to display on the form.
This is the form
<%= simple_form_for(#debt) do |f| %>
<%= f.error_notification %>
<!-- Debt fields -->
<div class="form-inputs">
<%= f.association :user %>
<%= f.input :amount %>
<%= f.input :commission %>
<%= f.input :invoice_issued %>
<%= f.input :invoice_date %>
<%= f.input :status %>
<%= f.input :details %>
</div>
<!-- Debtor Fields -->
<ul>
<%= f.fields_for :debtor do |debtor_form| %>
<li>
<%= debtor_form.association :user %>
<%= debtor_form.input :business_name %>
<%= debtor_form.input :abn %>
<%= debtor_form.input :first_name %>
<%= debtor_form.input :last_name %>
<%= debtor_form.input :email %>
<%= debtor_form.input :mobile_number %>
<%= debtor_form.input :phone_number %>
</li>
<% end %>
</ul>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Any help is appreciated.
EDIT
create_table "debts", force: :cascade do |t|
t.integer "user_id"
t.float "amount"
t.float "commission"
t.boolean "invoice_issued"
t.date "invoice_date"
t.string "status"
t.text "details"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
You need to build your debt's debtor or creditor in your controller:
#controller
def new
#debt = Debt.new
#debt.build_debtor
#bebt.build_creditor
end

Values of helper collection_select is being called twice

I have model Category:
class Category < ActiveRecord::Base
has_many :projects
end
Model project:
class Project < ActiveRecord::Base
belongs_to :category
validates :title, :description, :category, :skill, :payment, presence: true
validates :price, numericality: { only_integer: true }
PRICE_CATEGORIES = ['cat_price1', 'cat_price2', 'cat_price3', 'cat_price4']
end
projects/new
<%= form_for #project, url: {action: 'create'} do |f| %>
<p>
<%= f.label 'Titel' %>
<%= f.text_field :title %>
</p>
<P>
<%= f.label 'Budget' %>
<%= f.text_field :price %>
für
<%= f.select :price_category, Project::PRICE_CATEGORIES %>
</P>
<p>
<%= f.label 'Beschreibung' %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label 'Kategorie' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<P>
<%= f.submit 'erstellen' %>
</P>
<% end %>
schema.rb:
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "projects", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "price"
t.string "skill"
t.string "location"
t.string "payment"
t.boolean "anonymity"
t.string "file"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "price_category"
end
When calling <%= f.collection_select :category_id, Category.all, :id, :name %>
all items in dropdown list displaying twice. Cant undestand, why.
For example:
cat1
cat2
cat3
cat1
cat2
cat3
Thank for you help.

Rails Model Association with form

I am trying to achieve this:
It has a model called Customer that has details of the customers name
(first, middle and last) and telephone numbers (mobile and landline).
It has a model called Address that is linked to Customer. This model
will have details for a customers address (a number of address lines,
a country and a post/zip code).
A customers controller that supports two pages. The first will list
all customers in the database along with their associated address.
The second will display a form for the entry of a new customer along
with their address and allow for the creation of a new customer and
address record. It should be possible to move from either page to the
other without having to enter an URL in the browser navigation bar.
A number of points of note about this are as follows...
This is my code:
Migration:
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :address_line1
t.string :address_line2
t.string :address_line3
t.string :city
t.string :county_province
t.string :zip_or_postcode
t.string :country
t.string :customer_id
t.timestamps null: false
end
create_table :customers do |t|
t.string :first_name
t.string :middle_name
t.string :last_name
t.integer :mobile_number
t.integer :landline_number
t.timestamps null: false
end
end
end
Model:
class Address < ActiveRecord::Base
end
class Customer < ActiveRecord::Base
has_one :address
end
Form:
<%= form_for([#address,#customer]) do |f| %>
<% if #customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% #customer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :middle_name %><br>
<%= f.text_field :middle_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :mobile_number %><br>
<%= f.number_field :mobile_number %>
</div>
<div class="field">
<%= f.label :landline_number %><br>
<%= f.number_field :landline_number %>
</div>
<div class="field">
<%= f.label :address_line1 %><br>
<%= f.text_field :address_line1 %>
</div>
<div class="field">
<%= f.label :address_line2 %><br>
<%= f.text_field :address_line2 %>
</div>
<div class="field">
<%= f.label :address_line3 %><br>
<%= f.text_field :address_line3 %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :county_province %><br>
<%= f.text_field :county_province %>
</div>
<div class="field">
<%= f.label :zip_or_postcode %><br>
<%= f.text_field :zip_or_postcode %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.text_field :country %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Routes:
resources :addresses do
resources :customers
end
resources :customers do
resources :addresses
end
I haven't changed anything in my both controllers.
I am getting this error: undefined method `address_line1' for #
I don't understand where i am wrong. Any help? :( please
Schema:
ActiveRecord::Schema.define(version: 20150722160725) do
create_table "addresses", force: :cascade do |t|
t.string "customer_id"
t.string "address_line1"
t.string "address_line2"
t.string "address_line3"
t.string "city"
t.string "county_province"
t.string "zip_or_postcode"
t.string "country"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "addresses", ["customer_id"], name: "index_addresses_on_customer_id"
create_table "customers", force: :cascade do |t|
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.integer "mobile_number"
t.integer "landline_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

Resources