为 nil 调用 id,它会错误地为 4 -- 如果您真的想要 nil 的 id,请使用 object_id [英] Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

查看:31
本文介绍了为 nil 调用 id,它会错误地为 4 -- 如果您真的想要 nil 的 id,请使用 object_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.我现在正在学习 ROR 并且遇到了一些麻烦.我已经使用脚手架创建了 other_users 和 post 模型.other_users 有很多:帖子等等.这个想法是,当用户登录并创建一个帖子时,在 show 操作中显示创建这个帖子的用户的名称.我想知道是否有人可以给我打电话.

后控制器

def new@post = Post.new(:other_user_id => @other_user.id)response_to do |格式|format.html # new.html.erbformat.json { 渲染 json: @post }结尾结尾定义创建@post = Post.new(params[:post])response_to do |格式|如果@post.saveformat.html { redirect_to @post,注意:'帖子已成功创建.'}format.json { 渲染 json: @post, 状态: :created, location: @post }别的format.html { 渲染动作:新"}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾

帖子视图_form:

<%= form_for(:post, :url => {:action => 'create', :other_user_id => @other_user.id}) do |f|%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>

<div class="field"><%= f.label :title%><br/><%= f.text_field :title %>

<div class="field"><%= f.label :content%><br/><%= f.text_area :content %>

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

<%结束%>

我知道这有问题,但我对此很陌生,所以我无法自己弄清楚

解决方案

在第 2 行 @other_user 未知,因此它为零.我们试图从 nil 中获取 id,因此它会产生名为 called id for nil 的错误.初始化 @other_user 并重试.

I would like to know if someone could help me. I'm learning now ROR and have run in some troubles. I have created other_users and post model using scaffold. other_users has many:posts and so one. The idea is that, when the user is logged in and creates a Post, in show action shows the name of the user which created this post. I would like to know if someone can tel me with this.

Post controller

def new
    @post = Post.new(:other_user_id => @other_user.id)

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end
def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

Post view _form:

<%= form_for(:post, :url => {:action => 'create', :other_user_id => @other_user.id}) do |f| %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I know that something is wrong with that, but i'm quite new with that so i can't figure it out on my own

解决方案

At line 2 @other_user is not known so it is nil. We are trying to get the id from a nil so it yields error called called id for nil. Initialize @other_user and try again.

这篇关于为 nil 调用 id,它会错误地为 4 -- 如果您真的想要 nil 的 id,请使用 object_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆