我无法克服这个错误"undefined local variable or method `f' "有什么原因吗?



我正试图将的提交按钮放在另一个页面上,但我遇到了这个错误。我在我的展示中有以下内容。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
    @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 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
    @post = Post.find(params[:id])
end
def update
    @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

我甚至尝试过渲染局部,认为这可能会有所帮助,我已经尝试了几件事,但似乎无法解决这个问题。我确信它非常简单,我很想念它,但如果你知道如何修复它,那么请提前发布你的答案并感谢你!我还可以添加任何其他代码,你可能需要看到,可以帮助这个过程!

f仅在form_for块中可用,该块正与第一个<% end %>一起过早关闭。

<% form_for @post do |f| %>
      </div>
    <% end %> <!-- Closes form_for; remove this line  -->
    <p>
         <%= f.label :body %><br>
         <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>

编辑:

show.html.erb中的<button type="button" class="btn btn-primary"><%= f.submit %></button>也在表单块的上下文之外。在关闭<% end %>之前,将此行移动到_form.html.erb,您的错误应该会消失。

这将解决错误,但除非按照Eifion的建议修改打开标记,否则表单将不会输出。

我认为您应该使用

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

用等号代替

<% form_for @post do |f| %>

在你部分的第一行。

相关内容

最新更新