为 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
问题描述
我想知道是否有人可以帮助我.我现在正在学习 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 %>