我的rails应用程序中有3个模型,我正在尝试将它们与关联
我用设备创建的用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :userdetail
has_many :posts
has_many :topics, :through => :posts
end
这是我的主题模型
class Topic < ActiveRecord::Base
#relation between topics and post
has_many :posts
end
这是我的后模型
class Post < ActiveRecord::Base
#relation between topics and post
belongs_to :topic
belongs_to :user
#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 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
@current_user.posts << @topic.posts.build(params[:post])
#@topic.posts << @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, :user_id)
end
end
这是我发布的新视图
<%= form_tag topic_posts_path(@topic) do %>
<%= label :post, :description %>
<%= text_area :post, :description %>
<%= submit_tag 'Create post' %>
<%end%>
我在后控制器中得到了这个错误。我不确定为什么我会得到这个错误
nil的未定义方法"posts":@current_user.posts<的NilClass<topic.posts.build(params[:post])
我正在尝试将主题和帖子关联起来在我的专题节目中。
我使用了代码<%=link_to"写入",new_topic_post_path(topic_id:@topic)%>
<%= link_to 'Write', new_topic_post_path(topic_id: @topic) %>
我正在使用设备进行用户登录,但我没有在应用程序中的任何位置设置current_user。
我想创建一个应用程序,用户可以在其中创建关于主题的帖子。每个主题都有多个帖子。我是新来的,正在学习RoR,所以请解释答案
设备为您提供current_user
助手(无@
)。使用它,而不是@current_user
。
您可能还想设置
before_filter :authenticate_user
在你的帖子PostsController
中-确保current_user
不是nil(并且用户正确登录)。
创建方法的一部分也应该更改:
@post = @topic.posts.build(post_params)
@current_user.posts << @post