状态#new中的NoMethodError

时间:2014-03-29 02:26:32

标签: ruby-on-rails ruby devise

我目前正在大学里建立这个网站以获得乐趣。我希望它能帮助教育系统。无论如何,我还在试图找出轨道。我只是设置了设计宝石没有问题。 但是,当我点击发布新状态时,会出现此错误:

NoMethodError in Statuses#new

Showing /Users/wyatt/Network/netbook/app/views/statuses/_form.html.erb where line #16 raised:

undefined method `user_name' for #<Status:0x00000101ec36d0>
Extracted source (around line #16):

13: 
14:   <div class="field">
15:     <%= f.label :user_name %><br />
16:     <%= f.text_field :user_name %>
17:   </div>
18:   <div class="field">
19:     <%= f.label :content %><br />

所以这是 我的表单 的样子:

  <%= form_for(@status) do |f| %>
 <% if @status.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@status.errors.count, "error") %> prohibited this status from being    saved:</h2>

  <ul>
  <% @status.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
 <% end %>

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

这是我的控制器

  class StatusesController < ApplicationController
  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @statuses }
    end
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
    @status = Status.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/new
  # GET /statuses/new.json
  def new
    @status = Status.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/1/edit
  def edit
    @status = Status.find(params[:id])
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = Status.new(params[:status])

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

  # PUT /statuses/1
  # PUT /statuses/1.json
  def update
    @status = Status.find(params[:id])

    respond_to do |format|
      if @status.update_attributes(params[:status])
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status = Status.find(params[:id])
    @status.destroy

    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end
end

1 个答案:

答案 0 :(得分:0)

通过查看,您似乎想要将user_name附加到status

错误

您的错误基本上意味着您的user_name数据表中没有statuses列。基本修复将是创建迁移以向user_name db添加statuses列:

$ rails generate migration AddUserNameToStatuses

#db/migrations/AddUserNameToStatuses.rb
class AddUserNameToStatuses < ActiveRecord::Migration
  def change
      add_column :statuses, :user_name, :string
  end
end

$ rake db:migrate

<强>修正

我实际上会抛弃它,并且这样做:

#app/models/status.rb
Class Status < ActiveRecord::Base
    belongs_to :user #-> you need user_id column in statuses db
    delegate :name, to: :user, prefix: true
end

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :statuses
end

这样您就可以从表单中删除对user_name的引用 - 因为任何@status都会与user

相关联

然后,您可以使用.delegate()方法在user_name

中呼叫statuses