Microposts中的NoMethodError #create

时间:2013-06-11 14:14:23

标签: ruby-on-rails ruby nomethoderror

我的Ruby on Rails应用程序遇到了一些问题。我是新手,所以我想这个问题并不太复杂

基本上我正在尝试创建一个讨论板。页面/讨论由用户创建了许多讨论,这些用户可以使用微博来评论它们。当我点击微博表格的帖子按钮时,我收到一个错误,上面写着

Microposts中的NoMethodError #create

显示/home/nick/Documents/RailsProjects/buon/app/views/shared/_discussion_form.html.erb,其中第1行被提出:

NilClass的未定义方法`model_name':Class

任何想法?继承了一些代码


micropost.rb

class Micropost < ActiveRecord::Base
  attr_accessible :break_votes, :content, :not_votes
  belongs_to :user
  belongs_to :discussion


  validates :content, presence: true, length: { maximum: 200 }
  validates :user_id, presence: true
  validates :discussion_id, presence: true

  default_scope order: 'microposts.created_at DESC'

结束

_discussions.html.erb

<% content_for :script do %>
<%= javascript_include_tag 'hover_content' %>
<% end %>

<li>
  <div class = "intro-bar"><span class = "intro"><%=discussion.intro %></span></div>
  <div class = "content-bar">
   <span class = "content"><%= discussion.content %>
   <div class = "vote-plus"><%= link_to "Stay Together", signup_path,class: "btn btn-large btn-primary" %></div>
    <div class = "vote-neg"><%= link_to "Break Up", signup_path,class: "btn btn-large btn-primary" %></div>
   </span>
  </div>
  <span class = "timestamp">
    Posted <%= time_ago_in_words(discussion.created_at) %> ago.
  </span>
</li>

<div class = "comments">
<% discussion.microposts.each do |micropost| %>
    <li>
      <div class = "post-comment"><%= micropost.content%></div>
    </li>
    <% end %>
</div>

<% if signed_in? %>
<div class = "row">
  <aside class = "span4">
    <section>
      <%= render 'shared/micropost_form', :locals => {:discussion => discussion }%>
    </section>
  </aside>
</div>
<% end %>

微博控制器

class MicropostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]

  def index
  end

  def create
    @micropost = current_user.microposts.build(params[:micropost])
     if @micropost.save
       flash[:success] = "Posted!"
       redirect_to root_url
     else
       render 'static_pages/home'
     end
  end

  def destroy
  end
end

discussions_controller

class DiscussionsController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update]


  def show
    @user = User.find(params[:id])
    @discussions = @user.discussion.paginate(page: params[:page])
    @microposts = @user.micropost.paginate(page: params[:page])
  end

  def index
    @discussions = Discussion.all
  end

  def create
    @discussion = current_user.discussions.build(params[:discussion])
    if @discussion.save
      flash[:success] = "Discussion Started!"
      redirect_to root_url
    else
      render 'static_pages/home'
    end
  end

  def destroy
  end

  def edit
  end

  def update
  end

  def new
  end

结束

更新:micropost_form

<% @micropost = Micropost.new %>
<% @micropost.discussion = nil %>

<%= form_for(@micropost) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <div class="field">
      <%= f.text_area :content, placeholder: "Compose new micropost..." %>
    </div>
    <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

0 个答案:

没有答案