2013-09-27 37 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

相关问题