2015-12-15 34 views
-2

我正在嘗試爲產品創建一個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!

+2

歡迎計算器。你應該只包括相關的代碼。你不應該期望那些願意幫你篩選整個應用程序的人。在這種情況下,OfferController和'offering/show.html.erb'之外的所有內容都是不相關的。 – max

+0

你的模型是錯誤的,你應該在你的Offer類('offer.rb')中有'belongs_to:product',並且'accep_nested_attributes_for:offers'應該放在你的'Product'類('product.rb')中。 – Roma149

回答

1

您應該更改設置,以便在您點擊產品時獲得索引。你可以用nested resources做到這一點:

#config/routes.rb 
resources :products do 
    resources :offers, only: [:index, :show, :new, :create] #-> url.com/products/:product_id/offers 
end 

這將讓你「範圍」的報價流動圍繞product你看:

#app/controllers/offers_controller.rb 
class OffersController < ApplicationController 
    before_action :set_product 

    def index 
     @offers = @product.offers 
    end 

    def show 
     @offer = @product.offers.find params[:id] 
    end 

    private 

    def set_product 
     @product = Product.find params[:product_id] 
    end 
end 

這必須與相應的結合model associations

#app/models/product.rb 
class Product < ActiveRecord::Base 
    has_many :offers 
end 

#app/models/offer.rb 
class Offer < ActiveRecord::Base 
    belongs_to :product 
end 

從您的表的外觀上來看,你需要確保你有適當的foreign_keyoffersproduct_idoffers表):

enter image description here

-

這會給你訪問url.com/products/1/offers讓所有的報價該產品的能力,然後通過點擊url.com/products/1/offers/5看到具體的報價。


在問候你的錯誤,

沒有方法 'OFFER_NAME'

...您需要關於如何在Rails的工作屬性讀了。

每次調用一個實例變量的時候,你使用下列內容:

@product.product_name 
@product.product_desc 

這些錯誤

您需要爲每個對象引用各種屬性。屬性基本上是數據庫列名,在出於某種原因您添加了對象名之前,您已擁有這些名稱。

爲了擺脫錯誤的,你基本上需要參考屬性名稱本身:

<% @offers.each do |offer| %> 
    <tr> 
     <td><%= offer.id %></td> 
     <td><%= offer.name %></td> 
     <td><%= offer.start_date %></td> 
     <td><%= offer.end_date %></td> 
     <td><%= offer.description %></td> 
    </tr> 
<% end %>