未定义方法`content'用于#<status id:=“”nil,=“”created_at:=“”nil,=“”updated_at:=“”nil,=“”user_id:=“”nil =“”> < /状态>

时间:2013-07-03 15:53:56

标签: ruby-on-rails methods

我一直收到这个错误,我不知道为什么。我现在已经搜索并排除了无数个小时的故障。我甚至有其他发布网站检查我的问题,他们甚至无法解决它。请帮忙。

错误:

NoMethodError in Statuses#new

Showing /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597/app/views/statuses/_form.html.erb where line #19 raised:

undefined method `content' for #<Status id: nil, created_at: nil, updated_at: nil, user_id: nil>
Extracted source (around line #19):

16:   </div>
17: 
18:    <%= f.input :user_id, collections: User.all, label_method: :full_name %>
19:    <%= f.input :content %>
20:   <div class="form-actions">
21:     <%= f.button :submit %>
22:   </div>
Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597

Application Trace | Framework Trace | Full Trace
app/views/statuses/_form.html.erb:19:in `block in _app_views_statuses__form_html_erb__1439688890277572820_15629600'
app/views/statuses/_form.html.erb:1:in `_app_views_statuses__form_html_erb__1439688890277572820_15629600'
app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb___4558354498189940408_70153195258980'
app/controllers/statuses_controller.rb:29:in `new'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

应用程序/模型/ status.rb

class Status < ActiveRecord::Base
  attr_accessible :content, :user_id
  belongs_to :user
end

应用程序/视图/状态/ _form.html.erb

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

配置/ routes.rb中

MGN::Application.routes.draw do
  devise_for :users

  resources :statuses
  root to: 'statuses#index'


  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

应用程序/视图/状态/ _form.html.erb

<%= simple_form_for(@status, html: {class: "form-horizontal"}) 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">

  </div>

   <%= f.input :user_id, collections: User.all, label_method: :full_name %>
   <%= f.input :content %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

这些都是以前的人帮我要求的。我不知道该怎么办,我失去了希望。 :(如果它有助于在我安装一个名为devise的gem时出现任何错误。当我安装devise时我无法迁移我的数据库,所以我删除了那个问题的表.Status表。Tutorial link

1 个答案:

答案 0 :(得分:0)

根据错误消息:

undefined method `content' for #<Status id: nil, created_at: nil, updated_at: nil, user_id: nil>

您必须编写一个迁移,向content表添加statuses列。

您可能已经进行了此类迁移,然后您应该运行rake db:migrate来执行此操作。