我无法为其发布帖子表单

时间:2015-02-25 04:28:59

标签: javascript ruby-on-rails twitter-bootstrap posts

我试图让用户点击他们帖子上的编辑,然后弹出bootstrap选项。它不会渲染表单,它给了我这个错误“未定义的局部变量或方法`f'”。

这是我在show.html.erb中的代码

<div class="post-page">
    <div class="panel panel-default">
        <div class="panel-heading-gray">
            <%= @user.name %> | <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Edit<%= link_to edit_post_path(@post) %></button> | <%= link_to 'Delete', post_path, method: :delete, data: { Confirm: "Are you sure?" } %><i class="fa fa-times"></i> 
        </div>
        <div class="panel-body"><h3><%= @post.body %></h3>
        </div>
           <div class="panel-footer">
            Posted <%= time_ago_in_words(@post.created_at) %> Ago
        </div>
    </div>

  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title" id="exampleModalLabel">Edit Post</h4>
    </div>
    <div class="modal-body">
     <form>
        <div class="form-group">
           <label for="message-text" class="control-label">Message:</label>
          <%= render 'form' %>
        </div>
  </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <%= f.submit %>
        </div>
      </div>
    </div>
  </div>

</div>

这是在我的_form.html.erb页面中,

 <% form_for @post do |f| %>
    <% if @post.errors.any? %>
      <div id="errors">
        <h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2>
        <ul>
            <% @post.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>

            <% end %>
        </ul>
      </div>
    <% end %>
    <p>
         <%= f.label :body %><br>
         <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit %>
    </p>
 <% end %>

我在post_controller.rb中有以下内容,

class PostsController < ApplicationController
    def index
        @user = User.find(session[:user_id])
        @posts = Post.all.order("created_at DESC")
    end
    def welcome
        @user = User.find(session[:user_id])
        @post = Post.find(params[:id])
        @posts = Post.order("created_at desc").limit(4).offset(1)
       @signed_in_user = session[:user_id]
    end
    def posts
        @user = User.find(session[:user_id]) unless session[:user_id] == nil
        redirect_to login_path, notice: "You're not logged in" unless @user 
    end
    def new 
        @post = Post.new
    end
    def create         
        @post = Post.new(post_params)

        @post.user_id = @signed_in_user

    if  @post.save 
        redirect_to dashboard_path 
    else
        render 'new'
    end
end
def show
    @user = User.find(session[:user_id])
    @post = Post.find(params[:id])
    @posts = Post.order("created_at desc").limit(4).offset(1)
    @signed_in_user = session[:user_id]

end
def edit
    @user = User.find(session[:user_id])
    @post = Post.find(params[:id])
end

def update
    @user = User.find(session[:user_id])
    @post = Post.find(params[:id])

    if @post.update(params[:post].permit(:body))
        redirect_to @post
    else
        render 'edit'
    end
    end
    def destroy
        @post = Post.find(params[:id])
        @post.user_id = @signed_in_user

        @post.destroy

        redirect_to posts_path
    end

    private
    def post_params
        params.require(:post).permit(:body)
    end
end

另外,这是在我的edit.html.erb页面上,

<div id="post-page">
    <h1>Edit Post</h1>

    <%= render 'form' %>

</div>

1 个答案:

答案 0 :(得分:0)

form_for要求您在ERB中使用<%=语法。使用您拥有的语法(<%),ERB将评估该语句,但不会打印出结果(例如,呈现的HTML)。

您的代码应如下所示:

<%= form_for @post do |f| %>

对于undefined local variable of method 'f',您在<%= f.submit %>中有一个迷路show.html.erb,这超出了表单的范围。因为它在范围之外,所以变量不再存在并且错误显示出来。