1

我正在制作一个 Rails 应用程序,这是我得到的错误。

> NoMethodError in Posts#index

Showing C:/Users/Corey/Dev/statlog/app/views/posts/_form.html.erb where line #1 raised:

undefined method `post_post_post_path' for #<#<Class:0x38109f0>:0x2c54c58>
Extracted source (around line #1):

1: <%= form_for(@posts) do |f| %>
2:   <div class="field">
3:     <h2><%= f.label 'So, hows it going?', :class => 'nuvo' %></h2><br />
4:     <%= f.text_area :status, :class => 'status-update' %>
Trace of template inclusion: app/views/posts/index.html.erb

Rails.root: C:/Users/Corey/Dev/statlog

Application Trace | Framework Trace | Full Trace
app/views/posts/_form.html.erb:1:in `_app_views_posts__form_html_erb___639176493_28084632'
app/views/posts/index.html.erb:10:in `_app_views_posts_index_html_erb___134739565_23263596'
app/controllers/posts_controller.rb:7:in `index'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

我的posts_controller 是:

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

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

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

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

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

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

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

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

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

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

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

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

最后,我的 _form.html.erb 是:

<%= form_for(@posts) do |f| %>
  <div class="field">
    <h2><%= f.label 'So, hows it going?', :class => 'nuvo' %></h2><br />
    <%= f.text_area :status, :class => 'status-update' %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

真的!希望有人能帮忙!这也很好,然后我使用控制台,并添加了一个新帖子:status,然后它开始搞砸了!请给我一些帮助!

~CoreyPizzle

4

1 回答 1

0

您的表单是否正在尝试创建新帖子?您正在给她一个包含所有现有帖子而不是新帖子的变量。

将此添加到您的 controller#index 操作中:

@post = Post.new

您的表单应该处理新帖子(@post)而不是所有帖子(@posts):

<%= form_for(@post) do |f| %>
    <%= f.label 'So, hows it going?', :class => 'nuvo' %>
    <%= f.text_area :status, :class => 'status-update' %>
    <%= f.submit "Create new status update"%>
<% end %>
于 2013-03-17T11:47:08.733 回答