2014-11-08 52 views
0

我在我的Rails应用程序3款车型,我想他们交往与设备协会表示零未定义的方法`帖:NilClass

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%>

我收到此错误后controller.I我不知道为什么我收到此错误

未定义的方法`岗位为nil:NilClass for @ current_user.posts < < @ 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所以请解释回答

回答

1

设备为您提供current_user帮手(没有@)。使用它,而不是@current_user

您可能还需要设置

before_filter :authenticate_user 

在您的文章PostsController - 以确保current_user不是零(和用户登录正确)。

同时创建方法的一部分应改为:

@post = @topic.posts.build(post_params) 
@current_user.posts << @post 
现在
+0

其加载ActiveModel显示:: ForbiddenAttributesError – Adt 2014-11-08 18:41:35

+0

http://stackoverflow.com/questions/17335329/activemodelforbiddenattributeserror-when-creating-new-user 你可能要阅读一些Rails教程... – Esse 2014-11-08 18:42:15

+0

在params.require(:post).permit(:topic_id,:issue,:description,:rating,:user_id)中有任何问题 – Adt 2014-11-08 18:44:43

相关问题