0

I am trying to learn Rails and am making my first app and am running into this error:

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

with the highlighted source:

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

I am brand new to rails and i can't figure out what is wrong and I can't find any help online either. The app is a part management system for electronic components. The form gets filled out and the data is saved to the database for future reference/updating. Could someone please help?

Source code time:

parts/_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 %>

parts/new_ic.html.erb

<%= render 'ic_form' %>

parts/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 Pretty sure i screwed this one up too

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

  resources :parts
  root to: "parts#new_ic"

end

rake routes Output:

   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
4

1 回答 1

1

One problem is in this line:

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

link_to should look like this:

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

Try this instead:

#parts/new.html.erb
<%= link_to 'IC', root_path %>

in your routes, root GET / parts#new_ic is linking to your new_ic action. I'd disagree with the way you access it (via root) - but it will work if you want to access the new_ic action. Why is this your root route, though?

于 2013-09-27T22:07:02.927 回答