still going on my first rails program (ruby 2.0, rails 4.0). I've got a drop down that displays static options from a second model, but when I click on a selection from that drop down, nothing happens. Saving it to the record (presence_of validation turned off) just leaves the field empty.
I'm thinking something in controller or view? Any help?
Sorry for the massive code dump, but I'm so noob that I wasn't sure what was relevant
Contact.rb
class Contact < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex
belongs_to :color
accepts_nested_attributes_for :color
Color.rb
class Color < ActiveRecord::Base
has_many :contacts
accepts_nested_attributes_for :contacts
attr_accessible :name, :hex
end
contacts_controller.rb
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
before_filter :prepare_colors
# GET /contacts
# GET /contacts.json
def index
#contacts = Contact.all
end
# GET /contacts/1
# GET /contacts/1.json
def show
end
# GET /contacts/new
def new
#contact = Contact.new
end
# GET /contacts/1/edit
def edit
end
# POST /contacts
# POST /contacts.json
def create
#contact = Contact.new(contact_params)
respond_to do |format|
if #contact.save
format.html { redirect_to #contact, notice: 'Contact was successfully created.' }
format.json { render action: 'show', status: :created, location: #contact }
else
format.html { render action: 'new' }
format.json { render json: #contact.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
respond_to do |format|
if #contact.update(contact_params)
format.html { redirect_to #contact, notice: 'Contact was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #contact.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
#contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
#contact = Contact.find(params[:id])
end
def prepare_colors
#colors = Color.all
end
# Never trust parameters from the scary internet, only allow the white list through.
def contact_params
params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id)
end
end
views/contacts/_form.html.erb
%= form_for(#contact) do |f| %>
<% if #contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% #contact.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :zip_code %><br>
<%= f.number_field :zip_code %>
</div>
<div class="field">
<%= f.label :favorite_color %><br>
<%= f.collection_select(:color_id, #colors, :id, :name, :include_blank => "Please select") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and /views/contacts/index.html
h3>Listing contacts</h1>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Zip code</th>
<th>Favorite color</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #contacts.each do |contact| %>
<tr>
<td><%= contact.first_name %></td>
<td><%= contact.last_name %></td>
<td><%= contact.email %></td>
<td><%= contact.zip_code %></td>
<td><%= contact.favorite_color %></td>
<td><%= link_to 'Show', contact %></td>
<td><%= link_to 'Edit', edit_contact_path(contact) %></td>
<td><%= link_to 'Destroy', contact, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Contact', new_contact_path %>
Ok, I am not entirely sure what is relationship here, but it seems to me that you have two columns in your Contact model which are created for the same reason: favorite_color and colour_id. You are not setting favorite_color value, but you are trying to display it. I guess color_id is to be a foreign_key to contact's favurite color. if so, you need to rename assocaition:
belongs_to :favorite_color, class_name: 'Color'
Rename color_id column to favorite_color_id, and then inside your view:
# form
<div class="field">
<%= f.label :favorite_color %><br>
<%= f.collection_select(:favorite_color_id, #colors, :id, :name, :include_blank => "Please select") %>
</div>
# index
<td><%= contact.favorite_color.name %></td>
Strong Params
First of all, if you're using rails 4.0 you don't need this line:
attr_accessible :first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex
This has been replaced with strong_params now, so you should put this into the bottom of your contacts controller:
private
def contact_params
params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex, colors_attributes: [])
end
Fields_For
The next thing is that you need to include the fields_for tag in your form, like this:
<%= f.fields_for :colors do |color| %><br>
<%= f.collection_select(:color_id, #colors, :id, :name, :include_blank => "Please select") %>
<% end %>
I think that will work. Your code was pretty long, so I guess if you have problems implementing it, just leave a comment :)
Update
You need to add this to your new function:
# GET /contacts/new
def new
#contact = Contact.new
#contact.color.build
#colors = Color.all
end
Also, I don't know if this will work again, but you might want to edit your collection_select to have this code:
<%= f.fields_for :colors do |color| %><br>
<%= color.collection_select(:color, #colors, :id, :name, :include_blank => "Please select") %>
<% end %>
Finally, you should include the :id attribute in your nested_attributes:
private
def contact_params
params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex, colors_attributes: [])
end
Related
I'm very new to Rails development and having a problem saving multiple images/attachments to a model. My problem is that the code below is not actually saving to the item_images table when I submit the form. I am following This Article as a guide, though it seems to be a bit out of date. I feel I'm in a little over my head at this point so I hope someone can point out what I'm missing. Thanks!
I have the following models:
item.rb
class Item < ActiveRecord::Base
has_many :item_images, :dependent => :destroy
accepts_nested_attributes_for :item_images, :reject_if => lambda { |t| t['item_image'].nil? }
end
item_image.rb
class ItemImage < ActiveRecord::Base
belongs_to :item
has_attached_file :image,
:styles => { thumb: "100x100#", small: "400x400#", large: "700x700" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
My controller looks like this:
items_controller.rb
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
# GET /items
# GET /items.json
def index
#items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
end
# GET /items/new
def new
#item = Item.new
4.times {#item.item_images.build}
end
# GET /items/1/edit
def edit
4.times {#item.item_images.build}
end
# POST /items
# POST /items.json
def create
#item = Item.new(item_params)
respond_to do |format|
if #item.save
format.html { redirect_to #item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: #item }
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
respond_to do |format|
if #item.update(item_params)
format.html { redirect_to #item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #item }
else
format.html { render :edit }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
#item.destroy
respond_to do |format|
format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
#item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size)
end
end
form.html.erb
<%= form_for #item, html: { multipart: true } do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :available %><br>
<%= f.check_box :available %>
</div>
<div class="field">
<%= f.label :sort_shop %><br>
<%= f.number_field :sort_shop %>
</div>
<div class="field">
<%= f.label :sort_gallery %><br>
<%= f.number_field :sort_gallery %>
</div>
<div class="field">
<%= f.label :item_type %><br>
<%= f.text_field :item_type %>
</div>
<div class="field">
<%= f.label :size %><br>
<%= f.text_field :size %>
</div>
<%= f.fields_for :item_images do |builder| %>
<% if builder.object.new_record? %>
<div class="field">
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Try this in strong parameters in Items controller
params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size,item_images_attributes: [:image ])
than in ItemImage.rb add this line
belongs_to :item, optional: true,
and remove this line from Item.rb
:reject_if => lambda { |t| t['item_image'].nil? }
`
If you get any error please reply
After checkout we have a form to fill with data from the user, if not fill we are presented with errors, after fill the data if we click Place Order it is suppose that will be an update of database, but all data disappears from the form and display errors like the first time with no data.
Here is my code:
/app/views/carts/_cart.html.erb:
<h2>Your Cart</h2>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<!-- START_HIGHLIGHT -->
<%= button_to "Checkout", new_order_path, method: :get %>
<!-- END_HIGHLIGHT -->
<%= button_to 'Empty cart', cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
/app/controllers/orders_controller.rb:
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
#orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
if #cart.line_items.empty?
redirect_to store_url, notice: "Your cart is empty"
return
end
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
#order.add_line_items_from_cart(#cart)
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to store_url, notice:
'Thank you for your order.' }
format.json { render action: 'show', status: :created,
location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors,
status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
#order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:name, :address, :email, :pay_type)
end
#...
end
/app/views/orders/new.html.erb:
<div class="depot_form">
<fieldset>
<legend>Please Enter Your Details</legend>
<%= render 'form' %>
</fieldset>
</div>
/app/views/orders/_form.html.erb:
<%= form_for(#order) do |f| %>
<% if #order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#order.errors.count, "error") %>
prohibited this order from being saved:</h2>
<ul>
<% #order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<!-- START_HIGHLIGHT -->
<%= f.text_field :name, size: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :address %><br>
<!-- START_HIGHLIGHT -->
<%= f.text_area :address, rows: 3, cols: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :email %><br>
<!-- START_HIGHLIGHT -->
<%= f.email_field :email, size: 40 %>
<!-- END_HIGHLIGHT -->
</div>
<div class="field">
<%= f.label :pay_type %><br>
<!-- START_HIGHLIGHT -->
<%= f.select :pay_type, Order::PAYMENT_TYPES,
prompt: 'Select a payment method' %>
<!-- END_HIGHLIGHT -->
</div>
<div class="actions">
<!-- START_HIGHLIGHT -->
<%= f.submit 'Place Order' %>
<!-- END_HIGHLIGHT -->
</div>
<% end %>
/app/models/order.rb:
class Order < ActiveRecord::Base
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
has_many :line_items, dependent: :destroy
# ...
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
end
/app/models/line_item.rb:
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
belongs_to :cart
def total_price
product.price * quantity
end
end
Ok , it is solved, the problem is in app/models/order.rb
What I have when the update orders do not work
class Order < ActiveRecord::Base
has_many :line_items, dependent: :destroy
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
After change with attr_accessible :cart_id, :product_id, :product, :name, :address, :email, :pay_type
class Order < ActiveRecord::Base
attr_accessible :cart_id, :product_id, :product, :name, :address, :email, :pay_type
has_many :line_items, dependent: :destroy
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
Now, everything is working has expected.
Thanks
Regards
I am creating a customer service web app for our clients that allows them to submit new customer service tickets, once logged in. I have a login system that I created using the sorcery gem, and I have modified it to accept an additional field: client code.
We are assigning client codes to help prevent unauthorized users from creating accounts.
Upon login, it asks for the information like so:
Name:
Email:
Client Code: (we assign this)
Password:
My question is this, is there a way to only display customer service tickets to clients with the same client code? For example, The "Coca-Cola" clients would only see other "Coca-Cola" tickets and the "Pepsi" clients would only see other "Pepsi" tickets, etc.
Here is my Tickets Controller:
class TicketsController < ApplicationController
before_filter :require_login
def new
#ticket = Ticket.new
end
def create
#ticket = Ticket.new(ticket_params)
if
#ticket.save
redirect_to #ticket
flash[:notice] = "Your Ticket has been submitted. We will contact you very soon!"
else
flash[:notice] = "Something went wrong :("
render 'new'
end
end
def show
#ticket = Ticket.find(params[:id])
end
def index
#tickets = Ticket.all
end
def edit
#ticket = Ticket.find(params[:id])
end
def update
#ticket = Ticket.find(params[:id])
if #ticket.update(ticket_params)
redirect_to #ticket
else
render 'edit'
end
end
def destroy
#ticket = Ticket.find(params[:id])
#ticket.destroy
redirect_to tickets_path
end
private
def ticket_params
params.require(:ticket).permit(:name, :email, :phone, :help)
end
end
Here is the Ticket Index View:
<div class="panel panel-default">
<div class="panel-heading">Ticket Queue</div>
<div class="panel-body">
<table class="table">
<tr>
<th>Name</th>
<th>Phone</th>
<th></th>
<th></th>
</tr>
<% #tickets.each do |ticket| %>
<tr>
<td><%= ticket.name %></td>
<td><%= ticket.phone %></td>
<td><%= button_to "View or Edit", ticket_path(ticket), :class => "btn btn-primary btn-sm", :method => :get %></td>
<td><%= button_to "Delete", ticket_path(ticket), :class => "btn btn-primary btn- sm", :method => :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
</div>
Here is the New Ticket View:
<div class="panel panel-info">
<div class="panel-heading">
<div id="wrapper">
<h1 class="panel-title">New Ticket
<div id="first"><%= button_to "Back", root_path, :class => "btn btn-primary btn-sm", :method => :get %></div>
</div>
</h1>
</div>
<div class="panel-body">
<%= form_for :ticket, url: tickets_path do |f| %>
<p>
<%= f.label "Name:" %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label "Email:" %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :"Phone #:" %>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :"How can we help?" %>
<p><%= f.text_area :help, :cols=> 38, :rows => 8 %></p>
</p>
<p>
<button type="submit" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-envelope"></span> Submit Ticket</button>
</p>
<% end %>
</div>
</div>
Here is the User Model:
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates :password, length: { minimum: 3 }
validates :password, confirmation: true
validates :password_confirmation, presence: true
validates :code, inclusion: { in: %w(Client1, Client2), message: "Please enter a valid Client Code", :allow_nil => false}
validates :email, uniqueness: true
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
end
Here is the New User View:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label "Client Code" %><br />
<%= f.text_field :code %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the User Controller:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
skip_before_filter :require_login, only: [:index, :new, :create]
# GET /users
def index
#users = User.all
end
# GET /users/1
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
def create
#user = User.new(user_params)
if #user.save
redirect_to(:users, notice: 'User was successfully created')
else
render :new
end
end
# PATCH/PUT /users/1
def update
if #user.update(user_params)
redirect_to #user, notice: 'User was successfully updated.'
else
render :edit
end
end
# DELETE /users/1
def destroy
#user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :code)
end
end
Let me know if you need to see anything else, thank you!
From your example, I would think Users would have an additional database layer:
class User < ActiveRecord::Base
belongs_to :company
has_many :tickets, through: :companies
end
class Company < ActiveRecord::Base
has_many :users
has_many :tickets
end
class Ticket < ActiveRecord::Base
belongs_to :company
end
Then you can easily display tickets associated with each company
Updated version - I have taken the initial advice provided (thanks for that!) but I'm still having the same issue. I have updated everything below.
I have two models, products that belong to a store.
I'm attempting to display a related object's column (Store.name) in a couple of views for products that belong to a store and can't seem to get the store to save correctly. Please note: Still very new to this and learning.
Model for Product:
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :name, :url, :price
end
Model for Store:
class Store < ActiveRecord::Base
has_many :products
has_many :pins, through: :products
accepts_nested_attributes_for :products
end
Controller for Product:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new
#stores = Store.all
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render action: 'show', status: :created, location: #product }
else
format.html { render action: 'new' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :description, :imageurl, :url, :price, :Store_id)
end
end
Form for products
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name, "Name" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description, "Description" %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :imageurl, "Image" %>
<%= f.text_field :imageurl, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :url, "Web Address" %>
<%= f.text_field :url, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :price, "Price" %>
$<%= f.text_field :price, class: "form-control" %>
</div>
<div class="form-group">
<%= collection_select(:product, :Store_id, Store.all, :id, :name, {:prompt=> "Select A Store"}, {:class => "form-control"} ) %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
<%= params.inspect %>
View for products (show.html.erb):
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #product.name %>
</p>
<p>
<strong>Description:</strong>
<%= #product.description %>
</p>
<p>
<strong>Store Id:</strong>
<%= #product.Store_id %>
</p>
<p>
<strong>Store Name:</strong>
<%= #product.store.try(:name) %>
</p>
<p>
<strong>Image:</strong>
<%= #product.imageurl %>
</p>
<p>
<strong>Url:</strong>
<%= #product.url %>
</p>
<p>
<strong>Price:</strong>
$<%= #product.price %>
</p>
<%= params.inspect %>
<%= link_to 'Edit', edit_product_path(#product) %> |
<%= link_to 'Back', products_path %>
You'll notice that I have .try in place where I'm referencing product.store.name from product.store so that I stop getting the error listed in the subject of this post.
When I look up the product I'm viewing using the console I see that Store_id: 2
When I look up the store with id of 2 I see that Store_id: 1 - so there is a value present there.
I printed params on the show view and only get this: {"action"=>"show", "controller"=>"products", "id"=>"2"}.
Can anyone find what I'm missing in this whole set up to get product.store.name to display in my product views? Let me know if I can provide more info!
First, you should be using snake_case for attributes. UpperCamelCase is reserved for constant names in Ruby, that includes things like classes and modules. Update your code to not use UpperCamelCase style naming for attributes (eg. ProdDesc, ProdImageUrl). Also it is unnecessary to use prefixes like Prod* for attributes.
So instead of
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :ProdTitle, :ProdUrl, :ProdPrice
end
Your class will look like this:
class Product < ActiveRecord::Base
belongs_to :store
validates_presence_of :title, :url, :price
end
You can read more about Ruby and Rails naming conventions here:
http://rubylearning.com/satishtalim/ruby_names.html
https://github.com/bbatsov/rails-style-guide
I suspect that this causes the problem. You will also need to rename columns in database and change your controller and view code to reflect the change. If you can, please start again with your application.
You have taken name for field In database which are non conventional. By default The active record looks for store_id field in product table . Field name in a table should be snake case .so now you have to explicitly tell in product model the foreign key for store model
I have a rails model 'Guitar' and a rails model 'Photo.' I wanted to be able to attach multiple photos so I watched the railscasts for Paperclip, read the docs, etc. I've used Paperclip in the past to attach a single photo, but I wanted to do multiples this time. I don't have a problem updating the model, and it doesn't error out when I attach sample photos, but when I rails console to Photos.all, nothing comes back. My ultimate goal is to a polymorphic association with an 'Amps' model, but I really just wanted to get 'Guitars' working first.
I have paperclip added to the Gemfile and I've bundle installed, restarted the server, etc. I think I'm missing something stupid and I'm new to rails so please be gentle...
guitar.rb
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitars
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x600#' }
end
guitars_controller.rb
class GuitarsController < ApplicationController
before_action :set_guitar, only: [:show, :edit, :update, :destroy]
# GET /guitars
# GET /guitars.json
def index
#guitars = Guitar.all
end
# GET /guitars/1
# GET /guitars/1.json
def show
end
# GET /guitars/new
def new
#guitar = Guitar.new
3.times { #guitar.photos.build }
end
# GET /guitars/1/edit
def edit
#guitar = Guitar.find(params[:id])
3.times { #guitar.photos.build }
end
# POST /guitars
# POST /guitars.json
def create
#guitar = Guitar.new(guitar_params)
#guitar.user_id = current_user.id if current_user
respond_to do |format|
if #guitar.save
format.html { redirect_to #guitar, notice: 'Guitar was successfully created.' }
format.json { render action: 'show', status: :created, location: #guitar }
else
format.html { render action: 'new' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guitars/1
# PATCH/PUT /guitars/1.json
def update
respond_to do |format|
if #guitar.update(guitar_params)
format.html { redirect_to #guitar, notice: 'Guitar was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guitars/1
# DELETE /guitars/1.json
def destroy
#guitar.destroy
respond_to do |format|
format.html { redirect_to guitars_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guitar
#guitar = Guitar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id)
end
end
guitars show.html.erb
<p>
<strong>Make:</strong>
<%= #guitar.make %>
</p>
<p>
<strong>Model:</strong>
<%= #guitar.model %>
</p>
<p>
<strong>Year:</strong>
<%= #guitar.year %>
</p>
<p>
<strong>Color:</strong>
<%= #guitar.color %>
</p>
<p>
<strong>Serial:</strong>
<%= #guitar.serial %>
</p>
<p>
<strong>Price:</strong>
<%= #guitar.price %>
</p>
<p>
<strong>Condition:</strong>
<%= #guitar.condition %>
</p>
<p>
<strong>Kind:</strong>
<%= #guitar.kind %>
</p>
<p>
<strong>Bodykind:</strong>
<%= #guitar.bodykind %>
</p>
<p>
<strong>Frets:</strong>
<%= #guitar.frets %>
</p>
<p>
<strong>One owner:</strong>
<%= #guitar.one_owner %>
</p>
<p>
<strong>User:</strong>
<%= #guitar.user_id %>
</p>
<div class="thumb">
<% for asset in #guitar.photos %>
<%= link_to image_tag(photo.photos.url(:thumb)), photo.photo.url(:original) %>
<% end %>
</div>
<%= link_to 'Edit', edit_guitar_path(#guitar) %> |
<%= link_to 'Back', guitars_path %>
guitars _form.html.erb
<%= simple_form_for(#guitar, html: { class: 'form-horizontal', :multipart => true}) do |f| %>
<% if #guitar.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2>
<ul>
<% #guitar.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :make, label: 'Make of the Guitar', placeholder: 'Gibson' %>
<%= f.input :model, label: 'Model of the Guitar', placeholder: 'Les Paul' %>
<%= f.input :year, label: 'Year Made', placeholder: '1957' %>
<%= f.input :color, label: 'Color', placeholder: 'GoldTop' %>
<%= f.input :serial, label: 'Serial', placeholder: '#7-8789' %>
<%= f.input :price, label: 'Price' %>
<%= f.input :condition, label: 'Condition, 1-10' %>
<%= f.input :kind, label: 'Kind of Guitar', placeholder: '6-String-Electric' %>
<%= f.input :bodykind, label: 'Body Type', placeholder: 'Solid String Electric' %>
<%= f.input :frets, label: 'Number of Frets', placeholder: '22' %>
<%= f.input :one_owner, label: 'Original Owner' %>
<%= f.fields_for :photos do |photo| %>
<% if photo.object.new_record? %>
<%= photo.file_field :photo %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id, photos_attributes: [:photo])
end
You're running into mass assignment protection which is preventing the photos from being saved. Add this line to your Guitar model:
attr_accessible :photos_attributes
in my case I need create a document.
# config/initializer/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
This document is necessary create in config > initializers. If your SO is windows, you need add this line in config/environments/development.rb:
Paperclip.options[:comand_path] = "/ImageMagick-6.8.9-Q16/"