Ruby on rails Ruby On Rails关联错误缺少[:id]
我是RubyonRails的初学者,在尝试将TipList、Category和Tip这三个模型关联起来时遇到了错误。当用户创建新的提示列表时,他们将被带到类别的查看页面。然后,一旦用户为该提示列表创建了一个新类别,它会将用户带回所有类别的查看页面,并且浏览器会返回一个错误: 没有路线匹配{:action=>“show”,:controller=>“categories”,:format=>nil,:id=>nil,:tip_list_id=>#Category id:4,name:“stuff”,创建时间:“2015-02-13 23:26:12”,更新时间:“2015-02-13 23:26:12”,tip_list_id:4,name:“stuff”,创建时间:“2015-02-13 23:26:12”,tip_list_id:4>缺少必需的密钥:[:id] 我的模型看起来像:Ruby on rails Ruby On Rails关联错误缺少[:id],ruby-on-rails,Ruby On Rails,我是RubyonRails的初学者,在尝试将TipList、Category和Tip这三个模型关联起来时遇到了错误。当用户创建新的提示列表时,他们将被带到类别的查看页面。然后,一旦用户为该提示列表创建了一个新类别,它会将用户带回所有类别的查看页面,并且浏览器会返回一个错误: 没有路线匹配{:action=>“show”,:controller=>“categories”,:format=>nil,:id=>nil,:tip_list_id=>#Category id:4,name:“stuff”
class TipList < ActiveRecord::Base
has_many :categories, dependent: :destroy
has_many :tips, through: :categories
end
class Category < ActiveRecord::Base
belongs_to :tip_list
has_many :tips, dependent: :destroy
end
class Tip < ActiveRecord::Base
belongs_to :category
belongs_to :tip_list
end
class TipList
我的类别控制器
class CategoriesController < ApplicationController
before_action :set_category, only: [ :show, :edit, :update, :destroy ]
before_action :set_tip_list
before_action :authenticate_admin!
def index
@categories = @tip_list.categories.all
end
def show
@category = @tip_list.categories.find(params[:category_id])
end
def new
@category = @tip_list.categories.new
end
def edit
end
def create
@category = @tip_list.categories.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to tip_list_categories_path(@tip_list.id, @category) , notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_category
@category = @tip_list.category.find(category_params)
end
def set_tip_list
@tip_list = TipList.find(params[:tip_list_id])
end
def category_params
params.require(:category).permit(:id,:name)
end
end
class CategoriesController
类别索引的索引页如下所示
<p id="notice"><%= notice %></p>
<h1>Listing Categories for Tip List: <%= @tip_list.name %></h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', tip_list_category_path(category) %> </td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_tip_list_category_path %>
列出提示列表的类别:
名称
我一定是遗漏了什么,我的分类数据库表有一个提示列表id,所以我不知道哪里出错了。如果你能帮忙,那就太好了 如果您学会仔细观察rails提供给您的超级有用的错误,那么从长远来看,您会做得很好。阅读
{:action=>“show”、:controller=>“categories”、:format=>nil、:id=>nil、:tip_list_id=>#Category id:4、name:“stuff”、创建于:“2015-02-13 23:26:12”、更新于“2015-02-13 23:26:12”、tip_list_id:4、name:“stuff”、创建于“2015-02-13 23:26:12”、更新于“tip_list_id:4”
。查看:id
是如何nil
和:tip\u list\u id
有一个类别
对象,而不仅仅是一个整数?你知道为什么会这样吗?我已经知道:tip\u list\u id返回的是一个对象,而不是一个整数。当我将:tip\u list\u id列添加到Category表中时,我确保它是一个整数,我不知道它为什么会返回该值。好的,看看这个:
。我同意这应该行得通,但是不行。试试
。你需要告诉它旅行和类别。然后,您将在show
操作中得到一个新错误,因为category\u id
不是参数之一,您只需要id
。