我正在嘗試爲產品創建一個rails應用程序,它是優惠。我創建了一個產品頁面,通過它可以創建產品。這個頁面工作正常。但是,當我嘗試爲相應的產品創建商品頁面並將其重定向到列表頁面時,它會給我提供'NoMethodError in Offers#show Showing /Users/smita/workspace/products/app/views/offers/show.html .erb在行#9提出:「對零:NilClass'優惠中的NoMethod錯誤#show
未定義的方法`OFFER_NAME
我使用的軌道4,紅寶石2.2.3和MySQL數據庫
這裏是我的代碼:
products_controller.rb
class ProductsController < ApplicationController
def index
@product = Product.all
end
def list
@products = Product.order("products.product_name ASC")
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def create
# Instantiate a new object using form parameters
@product = Product.new(products_params)
# Save the object
if @product.save
# If save succeeds, redirect to the list action
redirect_to(:action => 'list')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def delete
@product = Product.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to :action => 'list', notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def products_params
params.require(:product).permit(:product_name, :product_desc, :product_image, :product_rate)
end
end
product.rb
class Product < ActiveRecord::Base
has_many :offers
end
CreateProducts
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string "product_name"
t.string "product_desc"
t.string "product_image"
t.integer "product_rate"
t.timestamps null: false
end
end
end
產品/ list.html.erb
<div>
<h2>Products</h2>
<%= link_to("Add New Product", {:action => 'new'}) %>
<table>
<tr>
<th>Product Id</th>
<th>Product Name</th>
<th>Product Desc</th>
<th>Product Image</th>
<th>Product Rate</th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%= product.id %></td>
<td><%= product.product_name %></td>
<td><%= product.product_desc%></td>
<td><%= product.product_image%></td>
<td><%= product.product_rate %></td>
<td>
<%= link_to("Show", {:action => 'show', :id => product.id}) %>
<%= link_to("Edit", {:action => 'edit', :id => product.id}) %>
<%= link_to("Delete", {:action => 'delete', :id => product.id}, data: { confirm: 'Are you sure you want to delete this entry ?' }) %>
</td>
</tr>
<% end %>
</table>
</div>
產品/ new.html.erb
<div>
<h2>Create Product</h2>
<%= form_for Product.new ,:url => {:action => :create, :controller => :products} do |f| %>
<table>
<tr>
<th>Product Name</th>
<td><%= f.text_field(:product_name) %></td>
</tr>
<tr>
<th>Product Desc</th>
<td><%= f.text_field(:product_desc) %></td>
</tr>
<tr>
<th>Product Image</th>
<td><%= f.text_field(:product_image) %></td>
</tr>
<tr>
<th>Product Rate</th>
<td><%= f.text_field(:product_rate) %></td>
</tr>
</table>
<div>
<%= submit_tag("Save Product") %>
</div>
<% end %>
</div>
個
產品/ show.html.erb
<%= link_to("<< Back to List", {:action => 'list'}) %>
<div>
<h2>Show Product</h2>
<table>
<tr>
<th>Product Name</th>
<td><%= @product.product_name %></td>
</tr>
<tr>
<th>Product Desc</th>
<td><%= @product.product_desc %></td>
</tr>
<tr>
<th>Product Image</th>
<td><%= @product.product_image %></td>
</tr>
<tr>
<th>Product Rate</th>
<td><%= @product.product_rate%></td>
</tr>
</table>
</div>
offers_controller.rb
class OffersController < ApplicationController
def index
@offer = Offer.all
end
def list
@offers = Offer.order("offers.offer_name ASC")
end
def show
@offer = Offer.find_by_id(params[:id])
end
def new
@offer = Offer.new
end
def create
# Instantiate a new object using form parameters
@offer = Offer.new(offers_params)
# Save the object
if @offer.save
# If save succeeds, redirect to the list action
redirect_to(:action => 'list', notice: 'offer was successfully created.')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def delete
@offer = Offer.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to :action => 'list', notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def offers_params
params.require(:offer).permit(:product_id, :offer_name, :offer_start_date, :offer_end_date, :offer_description)
end
end
offer.rb
class Offer < ActiveRecord::Base
belongs_to :offers
accepts_nested_attributes_for :offers
end
CreateOffers
class CreateOffers < ActiveRecord::Migration
def change
create_table :offers do |t|
t.string "product_id"
t.string "offer_name"
t.string "offer_start_date"
t.string "offer_end_date"
t.string "offer_description"
t.timestamps null: false
end
end
end
報價/ list.html。 erb
<div>
<h2>Offers !!!</h2>
<%= link_to("Add new offers", {:action => 'new'}) %>
<table>
<tr>
<th>Product Id</th>
<th>Offer Id</th>
<th>Offer Name</th>
<th>Offer start date</th>
<th>Offer start date</th>
<th>Offer description</th>
</tr>
<% @offers.each do |offer| %>
<tr>
<td><%= offer.id %></td>
<td><%= offer.offer_name %></td>
<td><%= offer.offer_start_date %></td>
<td><%= offer.offer_end_date %></td>
<td><%= offer.offer_description %></td>
<td>
<%= link_to("Show", {:action => 'show', :id => offer.id}) %>
<%= link_to("Edit", {:action => 'edit', :id => offer.id}) %>
<%= link_to("Delete", {:action => 'delete', :id => offer.id}, data: { confirm: 'Are you sure you want to delete this entry ?' }) %>
</td>
</tr>
<% end %>
</table>
</div>
報價/ new.html.erb
<div>
<h2>New Offers</h2>
<%= form_for Offer.new ,:url => {:action => :create, :controller => :offers} do |f| %>
<table>
<tr>
<th>Offer Name</th>
<td><%= f.text_field(:offer_name) %></td>
</tr>
<tr>
<th>Offer start date</th>
<td><%= f.text_field(:offer_start_date) %></td>
</tr>
<tr>
<th>Offer end date</th>
<td><%= f.text_field(:offer_end_date) %></td>
</tr>
<tr>
<th>Offer description</th>
<td><%= f.text_field(:offer_description) %></td>
</tr>
</table>
<div>
<%= submit_tag("Save offer") %>
</div>
<% end %>
</div>
報價/ show.html.erb
<%= link_to("<< Back to List", {:action => 'list'}) %>
<div>
<h2>Show offers</h2>
<table>
<tr>
<th>Offer Name</th>
<td><%= @offer.offer_name %></td>
</tr>
<tr>
<th>Offer start date</th>
<td><%= @offer.offer_start_date %></td>
</tr>
<tr>
<th>Offer end date</th>
<td><%= @offer.offer_end_date %></td>
</tr>
<tr>
<th>Offer description</th>
<td><%= @offer.offer_description %></td>
</tr>
</table>
</div>
請讓我知道我提前去wrong.Thanks!
歡迎計算器。你應該只包括相關的代碼。你不應該期望那些願意幫你篩選整個應用程序的人。在這種情況下,OfferController和'offering/show.html.erb'之外的所有內容都是不相關的。 – max
你的模型是錯誤的,你應該在你的Offer類('offer.rb')中有'belongs_to:product',並且'accep_nested_attributes_for:offers'應該放在你的'Product'類('product.rb')中。 – Roma149