在Controller#new

时间:2015-08-31 15:01:46

标签: ruby-on-rails activerecord

我有一个rails应用程序,其类别包含许多初创公司。每个创业公司都属于一个类别。当我点击启动新按钮时,我收到此错误

ActiveRecord::RecordNotFound in StartupsController#new
Couldn't find Category without an ID

这是启动控制器

class StartupsController < ApplicationController
  before_action :set_startup, only: [:show, :edit, :update, :destroy]
  respond_to :html

  def index
   @startups = Startup.all
  end


  show
  end


  def new
   @category = Category.find(params[:category_id])
   @startup = @category.startups.new
   respond_with(@startup)
   flash[:notice] = "Startup added."
 end


 def edit
 end


 def create
   @category = Category.find(params[:category_id])
   @startup = Startup.new(startup_params)
   @startup.saved
 end

 def update
   respond_to do |format|
    if @startup.update(startup_params)
    format.html { redirect_to @startup, notice: 'Startup was successfully updated.' }
    format.json { render :show, status: :ok, location: @startup }
  else
    format.html { render :edit }
    format.json { render json: @startup.errors, status: :unprocessable_entity }
    end
   end
 end

  def destroy
   @startup.destroy
   respond_to do |format|
   format.html { redirect_to startups_url, notice: 'Startup was successfully destroyed.' }
  format.json { head :no_content }
   end
  end

  private
    def set_startup
    @category = Category.find(params[:category_id])
    @startup = Startup.find(params[:id])
  end

  def startup_params
    params.require(:startup).permit(:category_id,:name, :date, :link)
  end
end

编辑:路由文件

的routes.rb

Rails.application.routes.draw do

   resources :startups
   resources :categories 

   root :to => 'categories#index'
end

初创公司form.html.erb

    <%= form_for(@startup) do |f| %>
      <% if @startup.errors.any? %>
     <div id="error_explanation">
      <h2><%= pluralize(@startup.errors.count, "error") %> prohibited this startup from being saved:</h2>

      <ul>
        <% @startup.errors.full_messages.each do |message| %>
       <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="field">
   <%= f.label :name %><br>
   <%= f.text_field :name %>
 </div>
  <div class="field">
   <%= f.label :date %><br>
   <%= f.text_field :date %>
 </div>
  <div class="field">
    <%= f.label :link %><br>
    <%= f.text_field :link %>
  </div>

  <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

Startup.rb模型

class Startup < ActiveRecord::Base
  belongs_to :category
end

分类模型 category.rb    类别&lt;的ActiveRecord :: Base的      has_many:初创公司    端

我错过了什么?

我不认为这是架构问题,因为我的初创公司中有category_id ..

编辑:这个想法是用户将能够1.创建一个新类别并将启动添加到该类别和/或将启动添加到现有类别。主页将是类别的索引,用户可以单击以查看类别并查看该类别中的所有初创公司。我有一个startups_controller和categories_controller。

编辑:所以我已经能够创建几个&#34;初创公司&#34;但他们没有获得类别ID。对我来说,最好的方法是确保在创建时为他们分配category_id。

1 个答案:

答案 0 :(得分:0)

在控制器的new方法中:

变化:

@category = Category.find(params[:category_id])

为:

@category = Category.new

您收到此错误的原因是params没有category_id,因此操作无法找到该类别。

更新

因此,您的categories有很多startups。而且,每个startup都属于category

categories模型中添加以下内容:

accepts_nested_attributes_for :startup

accepts_nested_attributes_for方法允许您使用Startup上的相同质量分配工具修改Category的实例,这使得简单表单变得如此微不足道。

现在,在您看来,添加以下内容:

<%= f.fields_for :startup do |ff| %>
  <div>
    <%= ff.label :name %>
    <%= ff.text_field :name %>
  </div>
  <div>
    <%= ff.label :link %>
    <%= ff.text_field :link %>
  </div>
  <div>
    <%= ff.label :date %>
    <%= ff.text_field :date %>
  </div>
<% end %>

基本上,通过这种方式,您可以构建嵌套表单并将categorystartup信息提交给控制器,然后就可以创建category和相应的{{1} }。

此外,您应该将startups作为表单中的外部对象,然后将@categoery嵌套在@startup内,如上所示,并应映射到@category' s categories_controller行动。

有关构建复杂嵌套表单的更多信息,请参阅此tutorial