I am making a rails app that users can use to shop for products. Everything was working fine, then I adjusted the products#index page, and now the data from the database is displaying on the html under the <div> of the page like so;
[#<Product id: 1, name: "Green", description: "coming from the hills of the emerald triangle, thi...", strain: "Sativa", price: #<BigDecimal:7fbe558f8228,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/NZkyXpr.jpg", created_at: "2015-04-12 22:56:42", updated_at: "2015-04-12 22:56:42", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>, #<Product id: 3, name: "Sour Diesel", description: "Sour, sometimes called Sour D, is an invigo...", strain: "Hybrid", price: #<BigDecimal:7fbe55902bd8,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/RUHZAXQ.jpg", created_at: "2015-04-13 01:42:35", updated_at: "2015-04-13 01:42:35", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>, #<Product id: 2, name: "Grand Daddy Purp", description: "Introduced in 2003 by Ken Estes, Granddaddy Purple...", strain: "Indica", price: #<BigDecimal:7fbe559015d0,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/8O5kXeL.jpg", created_at: "2015-04-13 01:41:17", updated_at: "2015-04-13 07:31:14", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>]
Here is my index page, along with my controller
index.html.erb
<section class="product-page">
<div class="container">
<div class="row text-center">
<%= #products.each do |product| %>
<div class="product col-md-4 text-center">
<h2><%= link_to product.name, product_path(product.id) %></h2>
<%= link_to image_tag(product.image_file_name),
product_path(product.id), :class => 'img-responsive' %>
<div class="product-info">
<div class="product-info-left"><%= product.descrip %></div>
<div class="product-info-right"><%= product.price %></div>
</div>
</div>
<% end %>
</div>
</div>
</section>
products_controller.rb
class ProductsController < ApplicationController
def index
#products = Product.all
end
def show
#product = Product.find(params[:id])
end
end
Does anyone know why the data from the database would be displaying on my html page? Did I forget to close a tag, is there something in my controller? I can also attach an image that shows what it looks like if my description isn't clear. Any tips would be much appreciated.
<%= #products.each do |product| %>
must be
<% #products.each do |product| %>
Related
I am new to Rails. I am developing a web application where a user inserts inventory of shoes. So the user enters style code, size, price, and quantity. I want quantity to define how many entries there are in the database. So, if the quantity is three, three separate rows would be created for each shoe. Currently each form submission creates one row in the database.
My create in my shoe_controller:
def create
#shoe = Shoe.new(shoe_params)
respond_to do |format|
if #shoe.save
format.html { redirect_to #shoe, notice: 'Shoe was successfully created.' }
format.json { render :show, status: :created, location: #shoe }
else
format.html { render :new }
format.json { render json: #shoe.errors, status: :unprocessable_entity }
end
end
end
My _form.html.erb
<%= form_with(model: shoe, local: true) do |form| %>
<% if shoe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(shoe.errors.count, "error") %> prohibited this shoe from being saved:</h2>
<ul>
<% shoe.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :sku %>
<%= form.text_field :sku %>
</div>
<div class="field">
<%= form.label :size %>
<%= form.text_field :size %>
</div>
<div class="field">
<%= form.label :quantity %>
<%= form.number_field :quantity %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
What changes would I need to make to achieve the result I am looking for?
Thanks!
#rohit is correct in that using the Shoe.create method will get you what you want. But to implement this, you can change your controller to the following. I'm sure there are much cleaner ways of doing this, but it should get you what you're looking for. Also, I would suggest validating the quantity in shoe_params is a positive integer.
def create
#show = Show.new(shoe_params)
# This will create an array of shoes from your params
new_shoes = (1..shoe_params[:quantity]).collect { Shoe.new(shoe_params) }
# Save each record and put the result in an array
success = new_shoes.map(&:save)
if success.all?
# all records saved
else
# maybe #shoe.valid? is false or something else happened
end
end
you can use ActiveRecord::Persistence#create, which can accept an array of hashes as a parameter.
Suppose quantity is three, so build the 3 new hash which will contain your records.
shoes_params = [
{
code: 1,
size: 8,
price: 300,
quantity: 1
},
{
code: 1,
size: 8,
price: 300,
quantity: 1
}, ...
]
Shoe.create(shoes_params)
I have seem this question posted before but the normal "add :content to model" doesn't work for my situation. I have already added it and the error still occurs.
This a modified version of a codecademy project if it looks familiar.
Model
class CreateNotes < ActiveRecord::Migration
def change
create_table :notes do |t|
t.text :content
t.timestamps
end
end
end
Controller
class NotesController < ApplicationController
def index
#notes = Note.all
end
def new
#note = Note.new
end
def create
#note = Note.new(note_params)
if #note.save
redirect_to '/notes'
else
render 'new'
end
end
private
def note_params
params.require(:note).permit(:content)
end
end
Route
Rails.application.routes.draw do
root 'notes#index'
get "notes" => "notes#index"
get "notes/new" => "notes#new"
post "notes" => "notes#create"
index.html.erb
<div class="header">
<div class="container">
<h1>Notes</h1>
</div>
</div>
<div class="notes">
<div class="container">
<% #notes.each do |note| %>
<div class="note">
<p class="content"><%= note.content %></p>
<p class="time"><%= note.created_at %></p>
</div>
<% end %>
<%= link_to 'New Note', "notes/new" %>
</div>
</div>
new.html.erb
<div class="header">
<div class="container">
<h1>Notes</h1>
</div>
</div>
<div class="create">
<div class="container">
<%= form_for(#note) do |f| %>
<div class="field">
<%= f.label :note %><br>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
</div>
</div>
If anyone can figure out why I am still getting this error after :content is already in the model, that would be awesome!
P.S. First post so sorry if it is terrible.
ActiveRecord creates the method content on the model by inspecting the columns in the database. Since the method is undefined, I think you may have forgotten to run migrations.
Possible reasons the column isn't present:
You aren't connecting to the same database when running migrations and when running the app. For example, you could be running in different environments.
Your migration doesn't actually add the needed column. Check your migration.
Possible ways of checking:
Inspect the database "by hand" to confirm that the column is actually present.
I'm making a random site to post gifs and images to for fun.
After I post something, the attributes show up under it, like this
[#Funpost id: 1, title: "Giphy Test", description: "HAHAHA SO FUNNY", url: "http://i.giphy.com/NuQ1Tz0fhqeEU.gif", created_at: "2015-01-20 00:21:34", updated_at: "2015-01-20 00:21:34">, #<Funpost id: 2, title: "test", description: "", url: "test", created_at: "2015-01-20 03:21:19", updated_at: "2015-01-20 03:21:19">
How do I get rid of it?
Thanks!!
You probably have an extra equals signs in your ERB code. e.g., if you do
<%= #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will end up displaying the funposts attributes as well as the title and description for each. However if you instead remove the equals sign from the first line
<% #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will only show the title and description for each funpost.
I want to access a model in my application.html.erb file. For this I defined a before_filter in the application_controller.rb:
before_filter :populate_ages
protected
def populate_ages
#ages= Ages.all
end
In my application.html.erb I have the following:
<%= #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
The names are rendered correctly but I get additional output which is the array #ages. It looks like this:
[#<ageid: 1, name: "Herren", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 2, name: "A-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 3, name: "B-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">] </ul>
I have this code in a navigation bar. So my navigation bar is extended by this additional text. Adding the loop in the body leads to the same output.
You don't want the = sign on the loop, change it to:
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
By having <%= #ages ... you're telling rails to display the array
Just write as
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
#ages is an Array instance. Now Array#each returns the receiver itself, when full iteration is completed. <%=.. %> means you are telling execute the each m,method and print the result of the #each method too, which is the problem, you have faced. Thus tell your template executed the method, don't print the result of it, and to do this correct syntax is <%.. %>.
I am watching on railscasts and tried to write the example with my own hands.
But I got an trouble with the step of: Creating The Form.
It requires creating association between Survey and Question.
But this association cannot be established in my rails application so no questions appear in the form
Here are the codes
Survey model:
class Survey < ActiveRecord::Base
attr_accessible :name, :questions
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions
end
Question model:
class Question < ActiveRecord::Base
attr_accessible :context, :survey_id
belongs_to :survey
end
Surveys_Controller method:
# GET /surveys/new
# GET /surveys/new.json
def new
#survey = Survey.new
3.times {#survey.questions.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: #survey }
end
end
question part in _form.html.erb
<% f.fields_for :questions do |builder|%>
<%= builder.label :context, "Question" %><br />
<%= builder.text_area :context, :rows => 3 %>
<% end %>
Here is what I got from testing in console
irb(main):010:0> #survey = Survey.new
=> #<Survey id: nil, name: nil, created_at: nil, updated_at: nil>
irb(main):011:0> 3.times {#survey.questions.build}
=> 3
irb(main):012:0> #survey.questions
=> [#<Question id: nil, survey_id: nil, context: nil, created_at: nil, updated_a
t: nil>, #<Question id: nil, survey_id: nil, context: nil, created_at: nil, upda
ted_at: nil>, #<Question id: nil, survey_id: nil, context: nil, created_at: nil,
updated_at: nil>]
You have a new object for model. You create 3 question for empty object. You questions must be empty. It no saved or defined.
If I understand your question correctly, you are not seeing questions in your form. You need to have one form for one question in your form. Are you doing questions.each in your form?
You need something like:
<% survey.questions.each do |question| %>
<%= fields_for question do |builder| %>
haha, i am being stupid.
It is a dumb question.
It doesn't matter about the association.
I made a typing mistake on
<% f.fields_for :questions do |builder|%>
it should return some texts inserting into HTML document rather than just processing as ruby code
it should be <%= f.fields_for :questions do |builder|%>
But thank you all of your answers :)