I would like with Rails4 to assign the current_user.email to a comment.
This comment is assign to a book.
[Page book][1] [1]: http://i.stack.imgur.com/2mEoG.png
I succeed to assign the current_user.email to a comment
but when i try to change the current_user with an other account, all the email of the comments change also. And i don't want that!
How can i resolve the problem?
Here some files of my app:
CommentsController:
class CommentsController < ApplicationController
def create
#book = Book.find(params[:book_id])
#comment = #book.comments.new(comment_params)
if #comment.save
redirect_to #comment.book, notice: I18n.t('books.comment')
else
render text: 'Error!'
end
end
private
def comment_params
params.require(:comment).permit(:message)
end
end
view:
<% if comment.message != nil %>
<li><strong><%= current_user.email %></strong></li>
<p><%= comment.message %></p>
<% end %>
form_for:
<%= form_for [#book, #comment] do |f| %>
<%= f.hidden_field :book_id %>
<%= f.text_area :message, :placeholder => "Message" %>
<br />
<%= f.submit %>
<% end %>
Table Comments:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :message
t.string :username
t.integer :book_id
t.integer :reader_id
t.integer :user_id
t.timestamps
end
end
end
Table Readers:
class CreateReaders < ActiveRecord::Migration
def change
create_table :readers do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
Table Books:
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.string :title
t.string :author
t.string :synopsis
t.timestamps
end
end
end
Well, if you want have assigned current user to comment you can place a hidden field to save users id (make sure you have configured associations in models):
form_for:
<%= form_for [#book, #comment] do |f| %>
...
<%= f.hidden_field :user_id, value: current_user.id %>
...
<% end %>
Update comment_params in controller:
private
def comment_params
params.require(:comment).permit(:message, :user_id)
end
and then, in view:
<% if comment.message != nil %>
<li>
<strong>
<%= comment.user.email %>
</strong>
</li>
<p><%= comment.message %></p>
<% end %>
That's it. If you need use reader model instead of user - you can do it in the same way..
Related
I am new to the rails community. I am working on an application where users can have username with a nested attribute of first and last name. The other text field associated with the User model works fine.
Any help would be much appreciated.
Attached are the app models, controllers, migration files, db schema, and views.
models
class User < ActiveRecord::Base
has_one :username, dependent: :destroy
accepts_nested_attributes_for :username, allow_destroy: true
end
class Username < ActiveRecord::Base
belongs_to :user
end
Migrations
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :about_me
t.string :nationality
t.string :sexe
t.timestamps null: false
end
end
end
class CreateUsernames < ActiveRecord::Migration
def change
create_table :usernames do |t|
add_column :username, :first_name, :string
add_column :username, :last_name, :string
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
controller
class UsersController < ApplicationController
def index
#all_users = User.all
#new_user = User.new
#username = Username.new
end
def new
#new_user = User.new
end
def create
User.create(user_params)
end
private
def user_params
params.require(:user).permit(:email, :about_me, username_attributes:
[:last_name, :first_name])
end
end
Views
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<%= form_for #new_user do |f| %>
<%= f.fields_for #new_user do |user| %>
<div class="field">
<%= user.label :email%>
<%= user.text_field :email %>
<%= user.label :about_me %>
<%= user.text_field :about_me %>
</div>
<% end %>
<%= f.fields_for :username do |name| %>
<div class="field">
<%= name.label :first_name %>
<%= name.text_field :first_name %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Try this
controller
def new
#new_user = User.new
#new_user.usernames.build
end
view
<%= form_for #new_user do |f| %>
# ...
<%= f.fields_for :usernames do |name| %>
<div class="field">
<%= name.label :first_name %>
<%= name.text_field :first_name %>
</div>
<% end %>
# ...
<% end %>
helper
def name_format(user_mst)
name = user_mst.first_name.capitalize
name += (" #{user_mst.last_name.capitalize}")
return name
end
to display on view
I am new to ruby on rails and coding in general.
I wrote a nested form_for where the Price class belongs to an Item class.
The values :amount, :timeframe, and :item_id are to be stored through the controller. After the action ends only the :timeframe and :item_id are stored but not the :amount value.
Does this have to do with:
The nested form?
The datatype, and or precision & scale details for :amount?
Could it also be an issue with my controller method?
Any clarification would be greatly appreciated!
Here is the form:
<%= form_for #item, html: {multipart: true} do |f| %>
<div class="main-form">
<div class="new-item-form">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="new-item-form">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="new-item-form">
<%= f.label :deposit %>
<%= f.text_field :deposit %>
</div>
<div class="new-item-form">
<%= f.label :tags %>
<%= f.text_field :tags %>
</div>
<div class="new-item-form">
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<%= fields_for :price do |f| %>
<div class="new-item-form">
<%=f.label :timeframe %>
<%= select_tag(:timeframe, options_for_select([["Hour"],["Day"],["Week"]])) %>
</div>
<div class="new-item-form">
<%=f.label :amount %>
<%=f.number_field :amount %>
</div>
<% end %>
<%= f.submit %>
<%end%>
This is the controller:
class ItemsController < ApplicationController
def show
#item = Item.find(params[:id])
#price = Price.where(item_id: #item.id).first
end
def new
#item = Item.new
end
def create
#item = Item.new(item_params)
if #item.save
#price = Price.create(:timeframe => params[:timeframe], :amount => params[:amount], :item_id => #item.id)
#price.save
redirect_to #item, notice: "Item Successfully Added!"
else
flash[:message] = "Something did not validate"
render 'new'
end
end
private
def item_params
params.require(:item).permit(:title, :description, :image, :user_id, :deposit, :tags)
end
end
Additional documents include the Item model and Price model:
class Item < ActiveRecord::Base
validates :title, :description, :deposit, :tags, presence: true
belongs_to :user
has_many :prices, :dependent => :destroy
has_many :reviews, :dependent => :destroy
has_many :ratings, :dependent => :destroy
has_many :users_that_reviewed_this, through: :reviews, source: :user
has_many :users_that_rated_this, through: :ratings, source: :user
end
class Price < ActiveRecord::Base
belongs_to :item end
As well as the database schema for these classes:
create_table "items", force: :cascade do |t|
t.string "title"
t.string "description"
t.string "image"
t.integer "user_id"
t.integer "deposit"
t.string "type"
t.string "tags"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "prices", force: :cascade do |t|
t.string "timeframe"
t.decimal "amount"
t.integer "item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
The problem in your code is in params[:amount]. No params are there, it should be params[:price][:amount]. It will work this way!
Though the approach is not the most "conventional"
Here is the correct approach using accepts_nested_attributes, adding prices_attributes to strong_parameters and using
In your model
class Item < ActiveRecord::Base
validates :title, :description, :deposit, :tags, presence: true
belongs_to :user
has_many :prices, dependent: :destroy
has_many :reviews, dependent: :destroy
has_many :ratings, dependent: :destroy
has_many :users_that_reviewed_this, through: :reviews, source: :user
has_many :users_that_rated_this, through: :ratings, source: :user
accepts_nested_attributes_for :prices, reject_if: :all_blank, allow_destroy: true
end
In your controller
def create
#item = Item.new(item_params)
#item.prices.build
if #item.save
redirect_to #item, notice: "Item Successfully Added!"
else
flash[:message] = "Something did not validate"
render 'new'
end
end
private
def item_params
params.require(:item).permit(:title, :description, :image, :user_id, :deposit, :tags, prices_attributes: [:timeframe, :amount])
end
In your view
<%= form_for #item, html: {multipart: true} do |f| %>
<div class="main-form">
<div class="new-item-form">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="new-item-form">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="new-item-form">
<%= f.label :deposit %>
<%= f.text_field :deposit %>
</div>
<div class="new-item-form">
<%= f.label :tags %>
<%= f.text_field :tags %>
</div>
<div class="new-item-form">
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<%= f.fields_for :prices, Price.new do |p| %>
<div class="new-item-form">
<%=p.label :timeframe %>
<%= p.select(:timeframe, [["Hour"],["Day"],["Week"]], :prompt => 'Select') %>
</div>
<div class="new-item-form">
<%=p.label :amount %>
<%=p.number_field :amount %>
</div>
<% end %>
<%= f.submit %>
<%end%>
I am trying to make a relationship between a user and a shipment model. Am using [devise][1] for generating users everything gone good but now am stopped at this. I am getting This error:
undefined method `first_name' for nil:NilClass
My models
Shipment.rb
class Shipment < ActiveRecord::Base
belongs_to :user
end
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
My controllers
Shipments_Controller.rb
class ShipmentsController < ApplicationController
before_action :set_shipment, only: [:show, :edit, :update, :destroy]
# GET /shipments
# GET /shipments.json
def index
#shipments = Shipment.all
end
# GET /shipments/1
# GET /shipments/1.json
def show
end
# GET /shipments/new
def new
#shipment = Shipment.new
end
# GET /shipments/1/edit
def edit
end
# POST /shipments
# POST /shipments.json
def create
#shipment = Shipment.new(shipment_params)
respond_to do |format|
if #shipment.save
format.html { redirect_to #shipment, notice: 'Shipment was successfully created.' }
format.json { render :show, status: :created, location: #shipment }
else
format.html { render :new }
format.json { render json: #shipment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shipments/1
# PATCH/PUT /shipments/1.json
def update
respond_to do |format|
if #shipment.update(shipment_params)
format.html { redirect_to #shipment, notice: 'Shipment was successfully updated.' }
format.json { render :show, status: :ok, location: #shipment }
else
format.html { render :edit }
format.json { render json: #shipment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shipments/1
# DELETE /shipments/1.json
def destroy
#shipment.destroy
respond_to do |format|
format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shipment
#shipment = Shipment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shipment_params
params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
end
end
My db Migration files are:
devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :city_name
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
def change
add_column :shipments, :user_id, :integer
add_index :shipments, :user_id
remove_column :shipments, :name
end
end
My Shipment Views files:
_form.html.erb
<%= simple_form_for(#shipment, html: {class: "form-horizontal"}) do |f| %>
<% if #shipment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>
<ul>
<% #shipment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id %>
<%= f.input :description %>
<div class="field">
<%= f.label :ship_from %><br>
<%= f.select :from, ['New York']%>
</div>
<div class="field">
<%= f.label :ship_to %><br>
<%= f.select :to, [ 'New york', 'Orlanda' ] %>
</div>
<%= f.input :date %>
<%= f.input :pay %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Index.html.erb
<div class="page-header">
<h1>Listing Shipments</h1>
</div>
<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>
<% #shipments.each do |shipment| %>
<div class="shipment">
<h3><strong><%= shipment.user.first_name %></strong></h3>
<h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
<div class="meta">
<%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
<%= link_to "show", shipment %>
<span class="admin">
| <%= link_to "Edit", edit_shipment_path(shipment) %> |
<%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
</span>
</div>
</div>
<% end %>
Show.html.erb
<%= notice %>
<p>
<strong>Name:</strong>
<%= #shipment.user.first_name %>
</p>
<p>
<strong>Shipment Description:</strong>
<%= #shipment.description %>
</p>
<p>
<strong>Shipment From:</strong>
<%= #shipment.from %>
</p>
<p>
<strong>Ship To:</strong>
<%= #shipment.to %>
</p>
<p>
<strong>Shipment Date:</strong>
<%= #shipment.date %>
</p>
<p>
<strong>Pay:</strong>
<%= #shipment.pay %>
</p>
<%= link_to 'Edit', edit_shipment_path(#shipment) %> |
<%= link_to 'Back', shipments_path %>
My command line show this log:
Started GET "/" for ::1 at 2015-07-28 16:07:24 +0530
Processing by ShipmentsController#index as HTML
Shipment Load (0.0ms) SELECT "shipments".* FROM "shipments"
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT
1 [["id", 1]]
Rendered shipments/index.html.erb within layouts/application (0.0ms)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `first_name' for nil:NilClass):
6:
7: <% #shipments.each do |shipment| %>
8: <div class="shipment">
9: <h3><strong><%= shipment.user.first_name %></strong></h3>
10: <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
11: <div class="meta">
12: <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
app/views/shipments/index.html.erb:9:in `block in _app_views_shipments_index_h
tml_erb__634505161_51269712'
app/views/shipments/index.html.erb:7:in `_app_views_shipments_index_html_erb__
634505161_51269712'
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within
rescues/layout (31.2ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_markup.html.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inline
d_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_s
tring (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/console.js.erb within layouts/javascript (31.2ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/index.html.erb (78.0ms)
It looks like a particular shipment has no user associated to it. For a quick fix, try the below code
<h3><strong><%= shipment.try(:user).try(:first_name) %></strong></h3>
How would I handle a belong_to with the following include. Instead of displaying the product_colour_id id like to show the associated colour (Update:Solved this part below). The product_colour_id is in the Product table and matches the corresponding Product_colour id.
Its the case of two or more has_many associations that i cant work out. Can it be done?
app/controller/home_controller.rb
class HomeController < ApplicationController
def index
products = Product.last(5)
product_ids = products.map(&:id)
#product_colour_ids = products.map(&:product_colour_id)
#allproduct_colours = ProductColour.all
#product_colour_map = ProductColour.find(#product_colour_ids)
#product_images = Product.includes(:product_images)
.where(product_images: {product_id: product_ids, :default_image => true})
end
end
/app/views/home/index.html.erb
<% #product_images.each do |pd| %>
<%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %>
<% pd.product_images.each do |i| %>
<div class="thumbnail">
<%= image_tag (i.product_image(:medium)) %>
<% end %> </div>
<div class="caption">
<h3><%= pd.product_name %></h3>
<p><%= pd.product_description %></p>
<p> <%= pd.product_colour_id %></p>
</div>
<% end %>
<% end %>
</div>
I'm having difficulty finding examples of multiple has_many includes. I assume there is a very straight forward pattern to it but cant work it out from apidock or api.rubyonrails.org. The problem i'm having is adding the Supply_company which is a has_many :through relationship to the one I already have with the product_image include.
Thankyou in advance for your advise
Update I have worked out how to display the belongs_to... feeling a little dumb on that as it was very easy just needed some time to think
<% pd.product_images.each do |pd| %>
<p> <%= pd.product_colour.product_colour %></p>
<% end %>
/app/models/product.rb
class Product < ActiveRecord::Base
belongs_to :product_type
belongs_to :product_category
belongs_to :product_colour
belongs_to :product_size
has_many :product_supply_companies, :foreign_key => 'product_id'
accepts_nested_attributes_for :product_supply_companies, :allow_destroy => true
has_many :supply_companies, :through => :product_supply_companies
accepts_nested_attributes_for :supply_companies
has_many :product_images, dependent: :destroy, :foreign_key => 'product_id'
accepts_nested_attributes_for :product_images, :allow_destroy => true
end
app/models/product_supply_company.rb
class ProductSupplyCompany < ActiveRecord::Base
belongs_to :product
belongs_to :supply_company
# accepts_nested_attributes_for :supply_company
# accepts_nested_attributes_for :product
end
app/models/supply_company.rb
class SupplyCompany < ActiveRecord::Base
has_many :products, :through => :product_supply_companies
has_many :product_supply_companies, :foreign_key => 'supply_company_id'
accepts_nested_attributes_for :products
accepts_nested_attributes_for :product_supply_companies, :allow_destroy => true
end
app/models/product_colour.rb
class ProductColour < ActiveRecord::Base
has_many :products
end
Database Schema
create_table "product_categories", force: true do |t|
t.string "product_category"
t.string "product_category_description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "product_colours", force: true do |t|
t.string "product_colour"
t.string "product_colour_description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "product_images", force: true do |t|
t.integer "product_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "product_image_file_name"
t.string "product_image_content_type"
t.integer "product_image_file_size"
t.datetime "product_image_updated_at"
t.boolean "default_image"
end
create_table "product_sizes", force: true do |t|
t.string "product_size"
t.string "product_size_description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "product_supply_companies", force: true do |t|
t.integer "product_id"
t.integer "supply_company_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "product_types", force: true do |t|
t.string "product_type"
t.string "product_type_description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products", force: true do |t|
t.string "product_name"
t.text "product_description"
t.integer "product_type_id"
t.integer "product_category_id"
t.string "product_colour_id"
t.integer "product_size_id"
t.datetime "created_at"
t.datetime "updated_at"
end
#products = Product.includes(:product_images, :colour, :supply_companies)
.where(product_images: {product_id: product_ids, :default_image => true})
.select('products.*, product_colours.product_colour')
this the query with all associations.
index.html.erb
<% #products.each do |pd| %>
<%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %>
<% pd.product_images.each do |i| %>
<div class="thumbnail">
<%= image_tag (i.product_image(:medium)) %>
</div>
<% end %>
<div class="caption">
<h3><%= pd.product_name %></h3>
<p><%= pd.product_description %></p>
<p><%= pd.product_colour %></p>
</div>
<% end %>
<% end %>
Product.rb
belongs_to :colour, class: 'ProductColor', foreign_key: 'product_colour_id'
For those that follow. User123 gave me some ideas that finally provided a solution. I had been over thinking the problem mostly.
app/controller/home_controller.rb
class HomeController < ApplicationController
def index
products = Product.last(5) ## find last five products
product_ids = products.map(&:id) ## map their ids so I can retrieve
images with matching id's
## create array of products with corresponding default_images.
## Not necessary to include has_many, has_many :through or
## belongs_to as they can be added directly on index.html.erb page
#products = Product.includes(:product_images)
.where(product_images: {product_id: product_ids, :default_image => true}).last(5)
end
end
/app/views/home/index.html.erb
<% #products.each do |pd| #1st level directly access product_attributes %>
<%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %>
<% pd.product_images.each do |i| # 2nd access has_many with
foreign key in different table %>
<div class="thumbnail">
<%= image_tag (i.product_image(:medium)) %>
</div>
<% end %>
<div class="caption">
<h3><%= pd.product_name %></h3>
<p><%= pd.product_description %></p>
<p><%= pd.product_colour.product_colour #first level access
belong_to relationship attributes %></p>
<p><% pd.supply_companies.each do |sc| # 2nd level again to
access has_many :through relationship only done again for
layout purposes. %></p>
<%= sc.company_name %>
<% end %>
</div>
<% end %>
<% end %>
I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is
undefined method `post_comments_path' for #<#<Class:0x2aef820>:0x2bd7df8>
Extracted source (around line #25):
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>...here it is line number 25
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
I have two model
post.rb
class Post < ActiveRecord::Base
has_many :comments
validates :title, presence: true,
length: { minimum: 5 }
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
I have two controllers
posts_controller.rb
class PostsController < ApplicationController
def new
#post=Post.new
end
def show
#post = Post.find(params[:id])
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :text))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
def create
#post = Post.new(post_params)
# it will also works #post=Post.new(params[:post].permit(:title,:text))
if #post.save
redirect_to #post
# or this command also works redirect_to action: :show, id: #post.id
else
render 'new'
end
end
def index
#posts = Post.all
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
comments_controller.rb
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(#post)
end
end
my routes.rb file is
Blog::Application.routes.draw do
resources :posts do
resources :comments
end
I have two migration files
create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :text
t.timestamps
end
end
end
create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
#above line sets foreign key column for the association between the two models.
t.timestamps
end
#and bellow add_index line sets up an index for this association column.
add_index :comments, :post_id
end
end
my show.html.erb file is
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
<strong>Text:</strong>
<%= #post.text %>
</p>
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit Post', edit_post_path(#post) %> |
<%= link_to 'Back to Posts', posts_path %>
I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is I used associations in the models whenever I am applying associations I am getting problem ,I am new to ruby as well as rails.I am using Rails4,Eclipse,Windows Xp(sp3) and mysql5.6 ,I am getting the above error when I want clicked the show link the error is
I have a feeling you missed the part about nested resources. As in:
resources :posts do
resources :comments
end
This means the routes generated by resources :comments are nested within your posts, which results in URLs like this:
/posts/1/comments