Rails错误未定义的方法`name'为nil:我的控制器中的NilClass

时间:2016-05-19 11:03:52

标签: ruby ruby-on-rails-4

我的Ruby on Rails代码存在一些问题。错误是:undefined method名称'为nil:NilClass`。

迁移:

class CreateFeedbacks < ActiveRecord::Migration
  def change
    create_table :feedbacks do |t|
      t.string :name
      t.text :body
      t.references :user, index: true

      t.timestamps
    end
  end
end

反馈模式:

class Feedback < ActiveRecord::Base
  belongs_to :user
end

用户模型:

has_many :feedbacks

档案:app/controllers/feedback_controller.rb

class FeedbacksController < ApplicationController    
  def index
    @feedback = Feedback.all
  end

  def create
    @feedback = @user.feedbacks.build(feedback_params)
    @feedback = Feedback.find(params[:id])
    if @feedback.save
      redirect_to my_profile_path
    end
  end

  def new
    @feedback = Feedback.new
  end

  def show
    @feedback = Feedback.find(params[:id])
  end

  private
  def feedback_params
    params.require(:feedback).permit(:name, :body)
  end
end

档案:views/feedbacks/_feedback.html.erb

<div class="info-frame-feedbacks">
  <p><strong><%= @feedback.name %></strong></p>
  <p><%= @feedback.body %></p>
  <p><%= time_ago_in_words(feedback.created_at) %> Ago</p>

  <h4>Add feedback:</h4>
  <%= render "feedbacks/form" %>
</div>

文件:`views / feedbacks / _form.html.erb``

<%= form_for ([@user, @user.feedbacks.build]) do |f| %>
    <p>
      <%= f.label :name %><br>
      <%= f.text_field :name %>
    </p>
    <p>
      <%= f.label :body %><br>
      <%= f.text_area :body %>
    </p>
    <br>
    <p>
      <%= f.submit %>
    </p>
<% end %>

视图呈现部分:

<%= render "feedbacks/feedback" %>

控制台日志:

  
    

ActionView :: Template :: Error(未定义的方法name' for nil:NilClass): 1: <div class="info-frame-feedbacks"> 2: <p><strong><%= @feedback.name %></strong></p> 3: <p><%= @feedback.body %></p> 4: <p><%= time_ago_in_words(feedback.created_at) %> Ago</p> 5: app/views/feedbacks/_feedback.html.erb:2:in _ app_views_feedbacks__feedback_html_erb__96950790565369491_70223374345140'app / views / profiles / _photographer_profile.html.erb:62:in     _app_views_profiles__photographer_profile_html_erb__2595614421496138366_70223370764320' app/views/profiles/my_profile.html.erb:1:in _ app_views_profiles_my_profile_html_erb ___ 130517334733103876_70223370684660'

  
     

渲染   /home/vlad/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb(1.5ms)呈现   /home/vlad/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb   (1.1ms)呈现   /home/vlad/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb   在救援/布局(14.4ms)内

1 个答案:

答案 0 :(得分:0)

你有几个错误。

你有错误的代码在create动作处理程序中填充@feedback。只需替换它:

@feedback = Feedback.find(params[:id])

用这个:

@feedback = Feedback.new

您还缺少控制器中的edit操作,因此默认只显示edit视图。你需要在那里设置@feedback变量。

尝试将此添加到您的控制器中,就在new操作处理程序的下方:

def edit
  @feedback = Feedback.find(params[:id])
end

应该这样做。