2013-09-27 39 views
0

我努力學習Rails和我做我的第一個應用程序,並正在到這個錯誤:的Rails 4.0的ActiveRecord :: RecordNotFound

ActiveRecord::RecordNotFound in PartsController#show 
Couldn't find Part with id=new_ic 

與高亮來源:

def set_part 
@part = Part.find(params[:id]) 
end 

我的品牌新的鐵軌,我無法弄清楚什麼是錯的,我也找不到任何幫助。該應用程序是電子元件的零件管理系統。填寫表格並將數據保存到數據庫以供將來參考/更新。有人可以幫忙嗎?

源代碼時間:

份/ _ic_form.html.erb

<h1>Add An IC</h1> 

<%= simple_form_for @parts do |f| %> 


    <%= f.input :component_type, :as => :hidden, :input_html => { :value => "IC"} %> 
    <%= f.input :ic_model, label: 'IC Model' %> 
    <%= f.input :ic_manufacturer, label: 'IC Manufacturer' %> 
    <%= f.input :ic_pinCount, label: 'IC Pin-Count' %> 
    <%= f.input :ic_mountType, collection: ["Through Hole", "Surface Mount"], label: 'IC Mount Type' %> 
    <%= f.input :ic_package, label: 'IC Package' %> 
    <%= f.input :ic_quantityOnHand, label: 'Quantity On Hand' %> 
    <%= f.input :ic_quantityOnOrder, label: 'Quantity On Order' %> 





    <%= f.button :submit %> 
<% end %> 

份/ new_ic.html.erb

<%= render 'ic_form' %> 

份/ new.html .erb

<h1>New part</h1> 

<%= link_to 'IC', 'new_ic' %> 

<%= link_to 'Back', parts_path %> 

parts_controller.rb

class PartsController < ApplicationController 
    before_action :set_part, only: [:show, :edit, :update, :destroy] 


    before_filter :initialize_parts 

    def initialize_parts 
    @parts = Part.new 
    end 


    # GET /parts 
    # GET /parts.json 
    def index 
    @parts = Part.all 
    end 

    # GET /parts/1 
    # GET /parts/1.json 
    def show 
    end 

    # GET /parts/new 
    def new 
    @part = Part.new 
    end 

    # GET /parts/1/edit 
    def edit 
    end 

    # POST /parts 
    # POST /parts.json 
    def create 
    @part = Part.new(part_params) 

    respond_to do |format| 
     if @part.save 
     format.html { redirect_to @part, notice: 'Part was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @part } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @part.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /parts/1 
    # PATCH/PUT /parts/1.json 
    def update 
    respond_to do |format| 
     if @part.update(part_params) 
     format.html { redirect_to @part, notice: 'Part was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @part.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /parts/1 
    # DELETE /parts/1.json 
    def destroy 
    @part.destroy 
    respond_to do |format| 
     format.html { redirect_to parts_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_part 
     @part = Part.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def part_params 
     params[:part] 
    end 
end 

的routes.rb肯定我搞砸這一個太

Pms::Application.routes.draw do 
    resources :parts 

    resources :parts 
    root to: "parts#new_ic" 

end 

耙路輸出:

Prefix Verb URI Pattern    Controller#Action 
    parts GET /parts(.:format)   parts#index 
      POST /parts(.:format)   parts#create 
new_part GET /parts/new(.:format)  parts#new 
edit_part GET /parts/:id/edit(.:format) parts#edit 
    part GET /parts/:id(.:format)  parts#show 
      PATCH /parts/:id(.:format)  parts#update 
      PUT /parts/:id(.:format)  parts#update 
      DELETE /parts/:id(.:format)  parts#destroy 
      GET /parts(.:format)   parts#index 
      POST /parts(.:format)   parts#create 
      GET /parts/new(.:format)  parts#new 
      GET /parts/:id/edit(.:format) parts#edit 
      GET /parts/:id(.:format)  parts#show 
      PATCH /parts/:id(.:format)  parts#update 
      PUT /parts/:id(.:format)  parts#update 
      DELETE /parts/:id(.:format)  parts#destroy 
    root GET /      parts#new_ic 
+0

你可以把耙路線的輸出? – dax

回答

1

的一個問題是在這一行:

<%= link_to 'IC', 'new_ic' %> 

link_to應該是這樣的:

link_to "Profile", profile_path(@profile) 
#Profile is the name 
#profile_path(@profile) is the link 

試試這個:

#parts/new.html.erb 
<%= link_to 'IC', root_path %> 
在你的路由

root GET / parts#new_ic被鏈接到您的new_ic的行動。我不同意你訪問它的方式(通過root) - 但是如果你想訪問new_ic行動,它會起作用。但爲什麼這是你的根路線?

+0

new_ic_path會是什麼? – smd75jr

+0

如果您查看'rake routes'的輸出,'new_ic'應該是爲'paths#new'指定的路徑 - 並且您使用'new_ic_path'將鏈接寫入此路徑 - 實際上只是看到您的更新,將會更改回答 – dax

+0

問題在於,我將針對不同類型的組件(如電阻器和電容器等)提供多種不同的形式,因此我希望零件/新頁面包含指向不同形式的鏈接(如new_ic) – smd75jr

相關問題