gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:26:18 25 4
gpt4 key购买 nike

我试图让我的提交按钮位于不同的页面上,但出现此错误。我的 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 %>

这是在我的 posts_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 中可用 block ,该 block 被第一个 <% 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 %>

编辑:

<button type="button" class="btn btn-primary"><%= f.submit %></button> show.html.erb 中的内容也位于表单 block 的上下文之外。将此行移至 _form.html.erb,在结束 <% end %> 之前您的错误应该会消失。

这将解决错误,但是除非您将开始标记修改为Eifion,否则您的表单将不会输出。 suggested .

关于javascript - 我无法克服这个错误 "undefined local variable or method ` f'“有什么原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com