Ruby on rails RoR-表单只工作一次
我正在构建一个论坛应用程序,用户可以在其中创建帖子并对其进行评论,此时帖子功能正常,但评论表单只工作一次,从那时起,帖子页面崩溃,显示以下错误: 邮展中的命名错误 未定义的方法“to_key”# 你是说?设置 到 通过rails控制台,我可以看到注释已保存 以下是终端错误:Ruby on rails RoR-表单只工作一次,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在构建一个论坛应用程序,用户可以在其中创建帖子并对其进行评论,此时帖子功能正常,但评论表单只工作一次,从那时起,帖子页面崩溃,显示以下错误: 邮展中的命名错误 未定义的方法“to_key”# 你是说?设置 到 通过rails控制台,我可以看到注释已保存 以下是终端错误: ActionView::Template::Error (undefined method `to_key' for #<Comment::ActiveRecord_Relation:0x007fb845e9cca8&
ActionView::Template::Error (undefined method `to_key' for #<Comment::ActiveRecord_Relation:0x007fb845e9cca8>
Did you mean? to_set
to_ary):
7: <h3>Reply to thread</h3>
8:
9: <div class="comments_form">
10: <%= form_for @comment, url: new_comments_path(@post), method: :post, remote: true do |f| %>
11: <%= f.label :title %><br>
12: <%= f.text_field :title, placeholder: "Type the comment title here" %><br>
13: <%= f.label :content %><br>
app/views/post/show.html.erb:10:in `_app_views_post_show_html_erb__4054771198415677123_70214711817160'
以及邮政署署长:
class PostController < ApplicationController
def index
if params[:user].blank?
@posts = Post.all.order("created_at DESC")
else
posts = Post.where(user_id: @user_id).order("created_at DESC")
end
end
before_action :authenticate_user!
before_action :find_post, only: [:show, :edit, :update, :destroy]
def show
if Comment.where(post_id: @post.id).present?
@comment = Comment.where(post_id: @post.id)
else
@comment = Comment.new
end
end
def new
@post = Post.new
end
def create
@post = Post.new
@post.user_id = current_user.id.to_i
#Post.create(title: title.string, content: content.string, user_id: current_user.id)
@post.assign_attributes(post_params)
if @post.save
flash[:notice] = "Successfully created post!"
#Post.create(title: title.string, content: content.string, user_id: current_user.id)
redirect_to show_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
def edit
end
def update
@post.assign_attributes(post_params)
if @post.changed?
@post.save
redirect_to show_path(@post)
else
render edit_path(@post)
end
end
def destroy
@post.destroy
redirect_to root_path#, notice: “Post destroyed”
end
private
def post_params
params.require(:post).permit(:title, :content, :user_id)
end
def find_post
@post = Post.find(params[:id])
end
end
class PostController
提前感谢您的帮助。我想替换这一行:
<%= form_for @comment, url: new_comments_path(@post), method: :post, remote: true do |f| %>
为此:
<%= form_for @post.comments.new, url: new_comments_path(@post), method: :post, remote: true do |f| %>
应该解决这个问题 应该提到的是,rails声称错误出现在表单的这一行:您能在之前添加相关的web控制台输出并包含错误吗?您好,刚刚添加了终端错误消息。页面是使用JS更新注释部分还是重新加载整个页面?确实如此,非常感谢!我是这样做的:但我认为你的解决方案更好!
devise_for :users
root to: 'post#index'
#Posts
get '/posts/new', to: 'post#new'
get '/post/:id', to: 'post#show', as: 'show'
get '/post/:id/edit', to: 'post#edit', as: 'edit'
post '/post', to: 'post#create'
put '/post/:id', to: 'post#update', as: 'update'
delete '/post/:id', to: 'post#destroy', as: 'delete'
#Comments
post '/post/:post_id', to: 'comment#create', as: 'new_comments'
class PostController < ApplicationController
def index
if params[:user].blank?
@posts = Post.all.order("created_at DESC")
else
posts = Post.where(user_id: @user_id).order("created_at DESC")
end
end
before_action :authenticate_user!
before_action :find_post, only: [:show, :edit, :update, :destroy]
def show
if Comment.where(post_id: @post.id).present?
@comment = Comment.where(post_id: @post.id)
else
@comment = Comment.new
end
end
def new
@post = Post.new
end
def create
@post = Post.new
@post.user_id = current_user.id.to_i
#Post.create(title: title.string, content: content.string, user_id: current_user.id)
@post.assign_attributes(post_params)
if @post.save
flash[:notice] = "Successfully created post!"
#Post.create(title: title.string, content: content.string, user_id: current_user.id)
redirect_to show_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
def edit
end
def update
@post.assign_attributes(post_params)
if @post.changed?
@post.save
redirect_to show_path(@post)
else
render edit_path(@post)
end
end
def destroy
@post.destroy
redirect_to root_path#, notice: “Post destroyed”
end
private
def post_params
params.require(:post).permit(:title, :content, :user_id)
end
def find_post
@post = Post.find(params[:id])
end
end
<%= form_for @comment, url: new_comments_path(@post), method: :post, remote: true do |f| %>
<%= form_for @post.comments.new, url: new_comments_path(@post), method: :post, remote: true do |f| %>