渲染'形式'给我一个ArgumentError

时间:2016-12-02 11:12:18

标签: ruby-on-rails ruby

我正在尝试创建一个活动供稿,并且相信我,因为我是ruby on rails的新手,所以需要一段时间才能创建。很抱歉有一个基本问题。

但是,我正在尝试渲染' _form.html.erb'因为我希望用户能够创建一个故事'并且它可以像任何活动源一样列在同一页面上。有人可以向我解释问题是什么吗?

我的index.html.erb:

    <p id="notice"><%= notice %></p>


    <h1>This is a list of posts</h1>

    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>User</th>
                <th colspan="3"></th>
            </tr>
        </thead>

        <tbody>
            <% @stories.each do |story| %>
            <tr>
            <td><%= story.name %></td>
            <td><%= story.description %></td>
            <td><%= story.user.email %></td>
            <td><%= link_to 'Show', story %></td>
            <% if user_signed_in? %>
            <td><%= link_to 'Edit', edit_story_path(story) %></td>
            <td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?'} %></td>
            <% end %>
            </tr>
            <% end %>

        </tbody>
    </table>

  <%= link_to 'New Story', new_story_path %>

  <%= render 'form' %>

_form.html.erb:

<%= form_for @story do |f| %>
  <%= render 'shared/errors', object: @story %>

  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :description %>
    <%= f.text_area :description, class: 'form-control', required: true, cols: 3 %>
  </div>

  <%= f.submit 'Create Story', class: 'btn btn-primary' %>
<% end %>

故事控制器:

class StoriesController < ApplicationController
    before_action only: [:destroy, :show, :edit, :update]


def index
    @stories = Story.order('created_at DESC')
end

def new
    @story = current_user.stories.build
end

def create
    @story = current_user.stories.build(story_params)
    if @story.save
        flash[:success] = "Your beautiful story has been added!"
        redirect_to root_path
    else
        render 'new'
    end
end

def edit
    @story = Story.find(params[:id])
end

def update
    @story = Story.find(params[:id])
    if @story.update_attributes(params.require(:story).permit(:name, :description))
        flash[:success] = "More knowledge, more wisdom"
        redirect_to root_path
    else
        render 'edit'
    end
end

def destroy
    @story = Story.find(params[:id])
    if @story.destroy
        flash[:success] = "I think you should have more confidence in your storytelling"
        redirect_to root_path
    else
        flash[:error] = "Can't delete this story, sorry"
    end
end

def show
    @stories = Story.find(params[:id])
end

private

def story_params
    params.require(:story).permit(:name, :description)
end



end

终端我的错误:

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
    1: <%= form_for @story do |f| %>
    2:   <%= render 'shared/errors', object: @story %>
    3: 
    4:   <div class="form-group">

1 个答案:

答案 0 :(得分:0)

您应该将@store变量添加到index操作。

def index
  @stories = Story.order('created_at DESC')
  @story = current_user.stories.build
end
相关问题