我的 Rails 应用程序中有两个模型帖子和主题
class Post < ActiveRecord::Base
#relation between topics and post
belongs_to :topic
#post is valid only if it's associated with a topic:
validates :topic_id, :presence => true
#can also require that the referenced topic itself be valid
#in order for the post to be valid:
validates_associated :topic
end
和
class Topic < ActiveRecord::Base
#relation between topics and post
has_many :posts
end
我正在尝试在它们之间建立关联。
我想要每个主题对应的多篇帖子
我使用了嵌套路由
Rails.application.routes.draw do
# nested routes
resources :topics do
resources :posts
end
resources :userdetails
devise_for :users, :controllers => { :registrations => "registrations" }
我的 Post Controller 看起来像
class PostsController < ApplicationController
# before_action :set_post, only: [:show, :edit, :update, :destroy]
before_filter :has_userdetail_and_topic, :only =>[:new, :create]
# GET /posts
# GET /posts.json
#for new association SAAS book
protected
def has_userdetail_and_topic
unless(@topic =Topic.find_by_id(params[:topic_id]))
flash[:warning] = 'post must be for an existing topic'
end
end
public
def new
@post = @topic.posts.build
#@@topic = Topic.find(params[:topic_id1])
end
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#@topic.posts << @post
#@current_user = current_user.id
@current_user.posts << @topic.posts.build(params[:post])
#@post = Post.new(post_params )
#@post.userdetail_id = current_user.id
#Association functional between topic and post
#Class variable used
#@@topic.posts << @post
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:topic_id,:issue, :description, :rating, :userdetail_id)
end
end
我正在尝试通过代码 <td><%= link_to 'Write', new_topic_post_path(@topic) %> </td>
从主题/索引导航
但是当我尝试访问localhost:3000/topics]
时
我收到错误
No route matches {:action=>"new", :controller=>"posts", :topic_id=>nil} missing required keys: [:topic_id]
任何人都可以告诉我这个错误吗,因为我是 Rails 新手,请明确指定答案。 我还有一个疑问,请告诉我是否错误地关联了主题和帖子。我对这行代码感到困惑 -
@topic.posts << @post
最佳答案
错误缺少必需的键:[:topic_id]
告诉您的是,您需要提供带有键topic_id
的哈希:
<%= link_to 'Write', new_topic_post_path(topic_id: @topic) %>
将资源传递给路由助手仅适用于 id
参数:
<%= link_to @topic, topic_path(@topic) %>
是一种简写形式:
<%= link_to @topic, topic_path(id: @topic.to_param) %>
添加:
@prcu 也是正确的。 @topic
记录需要保存到数据库中。未保存的记录没有 id,因为数据库在插入记录时分配 id 列。
您还需要在 PostsController 中设置 @topic
实例变量:
@topic = Topic.find(params[:id])
这通常是通过 before 过滤器完成的:
before_filter :set_topic, only: [:new]
def set_topic
@topic = Topic.find(params[:id])
end
同样的事情也需要在TopicsController#index
中完成。
关于ruby-on-rails - Rails 关联 new_path 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808145/