1

我在 routes.db 中有一行:

resources :specificationItems 

我的 specification_item.rb 模型

class SpecificationItem < ActiveRecord::Base
    attr_accessible :amount, :item_id, :price, :specification_id, :total
    belongs_to :specification
    belongs_to :item
end

specification_items.rb 控制器:

class SpecificationItemsController < ApplicationController
    def new

        @specificationItem = SpecificationItem.new(:specification_id => params[:specification_id])
    end

  def create

    @specificationItem = SpecificationItem.new(params[:specificationitem])

    if @specificationItem.save
        respond_to do |format|
        format.html {
            flash[:success] = "Запись добавлена успешно"
            redirect_to @specificationItem      
        }         
        format.js
        end
    else
      render 'new'
    end
  end

    def show
        @specificationitems = SpecificationItem.where("specification_id = ?", @specification.id.to_s)
    end

    def index
        @specificationitems = SpecificationItem.all
    end

end

使用规范/显示中的这一行,我调用了一个新操作specification_item

  <%= link_to "Add item to spec", new_specificationItem_path(:specification_id =>@specification.id), id: "new_item", remote: true %>

这是 new.js.erb 中的一个 js 动作

  $('#new_item').hide().after('<%= j render("form") %>')

部分 _form.html.erb:

  <%= form_for @specificationItem, remote: true do |f| %>
    <div class="row">
    <div class="span6 offset3">
        <%= f.label :item_id, "Товар" %>
        <%= f.text_field :item_id %>
        <%= f.hidden_field :specification_id %>

   </div>
   </div>

<%= f.submit "Сохранить", class: "btn btn-large btn-primary" %>
<% end %>

它引发错误:

ActionView::Template::Error (undefined method `specification_items_path' for #<#<Class:0xb5b708e4>:0xb5b6e5e4>):
    1: <%= form_for @specificationItem, remote: true do |f| %>
    2:     <div class="row">
    3:      <div class="span6 offset3">
    4:              <%= f.label :item_id, "Товар" %>
  app/views/specification_items/_form.html.erb:1:in `_app_views_specification_items__form_html_erb___537109236__624411928'
  app/views/specification_items/new.js.erb:1:in `_app_views_specification_items_new_js_erb___211974400__624460778'

我怀疑错误属于命名约定,但无法识别。对不起我的新手问题和错误的英语

谢谢!

4

1 回答 1

3

试试rake routes它会显示所有可用的路径。另外我注意到你写resources :specificationItems的,由于方便,它应该是resources :specification_items

于 2012-12-24T07:25:50.073 回答