gpt4 book ai didi

ruby-on-rails - Rails验证或可能的路由错误

转载 作者:行者123 更新时间:2023-12-03 08:17:43 25 4
gpt4 key购买 nike

我正在建立一个论坛时遇到麻烦。
我在我的评论模型中添加了一个validates:content,present:true,现在我得到了一系列错误。
我现在遇到的问题是:
enter image description here
仅当用户在不添加任何内容的情况下评论其OWN帖子时才会发生。如果他们在另一个用户的帖子中破坏了验证,则错误消息将正确呈现。
为什么评论验证会导致与编辑帖子路径相关的崩溃?两者不应该相关。
这是发生崩溃的页面。

<div class="conversations-content-items forum-bg">
<div class="post-main-container post-show-border">
<div class="post-group">
<div class="post-header">
<div class="post-creation-time">
<p>Posted by: <%= @post.user.username %> at <%= @post.created_at.strftime("%l:%M %p") %></p>
</div>
<% if @post.user == current_user %>
<%= link_to edit_post_path do %>
<i class="fas fa-edit icon-shrink"></i>
<% end %> <!-- link to edit -->
<% end %> <!-- if @user -->
</div>
<div class="post-title">
<p><%= @post.title %></p>
</div>
<div class="post-content">
<p><%= @post.content %></p>
</div>
</div>
<div class="post-toolbar">
<span class="post-comment-icon">
<img src="https://img.icons8.com/material-two-tone/24/000000/topic.png"/>
</span>
<span class="number-of-comments">
<%= @post.comments.size %> <p class='ml-1'>Comments</p>
</span>
</div>
</div>
<div class="comments-container">
<%= render 'comments/comment_form', post: @post, comment: @comment %>
<div class="comments-display">
<% @post.comments.each do |parent_comment| %>
<%= render 'comments/comment', post: @post, comment: parent_comment, parent_comment: parent_comment %>
<div class="comment-replies">
<% parent_comment.replies.each do |reply| %>
<%= render 'comments/comment', post: @post, comment: reply, parent_comment: parent_comment %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>

这是评论 Controller

class CommentsController < ApplicationController
def new
@comment = Comment.new
authorize @comment
end

def create
@post = Post.find(params[:post_id])
@language = @post.language
@comment = Comment.new(comment_params)
@comment.user = current_user
@comment.post = @post
new_cp_total = current_user.convo_points + 2
current_user.update(convo_points: new_cp_total)
authorize @comment
if @comment.save
redirect_to post_path @post
else
render "posts/show", layout: "conversations"
end
end

def show
@comment = @post.comment
authorize @comment
end

private

def comment_params
params.require(:comment).permit(:content, :post_id, :user_id, :comment_id)
end
end

评论提交的地方

<div class="comment-form-hub">
<%= render 'devise/shared/error_messages', resource: comment %>
<%= form_for [post, comment] do |f| %>
<%= f.text_field(:comment_id, value: parent_comment&.id, hidden: true) if defined?(parent_comment) %>
<div class="post-create-comment">
<%= f.text_area :content, placeholder: "Share your thoughts...", class: "comment-form" %>
</div>
<div class="post-comment-submit-wrapper">
<%= f.submit "Comment", class: "comment-button border-glow" %>
</div>
<% end %>
</div>

这是路线

Rails.application.routes.draw do

root to: 'pages#home'

mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
post 'messages', to: 'messages#create'

devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
}

authenticate :user, ->(user) { user.admin? } do
mount Blazer::Engine, at: "blazer"
end

resources :languages, only: :index, path: "/", param: :lang do
member do
resources :posts, only: :index, path: 'forum', as: :forum
resources :posts, only: :new
resources :messages, only: :index, path: 'chat', as: :chat
end
end

get '/leaderboards', controller: 'pages', action: 'leaderboards'

post '/contact/send', controller: 'contact', action: 'send_message'

resources :posts, only: [ :create, :show, :edit, :update ] do
resources :comments, only: [:create, :destroy]
end

resources :messages, only: :create

resources :users, only: :show

resources :games, only: [ :show, :index ] do
resources :plays, only: :create
end

# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

最佳答案

edit_post_path url帮助器必须至少接收您要编辑的帖子的ID,否则该帮助器将无法推断出如何生成该URL。
尝试以下方法:

<%= link_to edit_post_path(@post.id) do %>
多一点解释:
任何此类REST资源的url帮助器都必须具有 ID 标识符以生成URL,因此:
edit_post_path(36)将生成URL:
/posts/36/edit
edit_comment_path(8)将生成URL:
/comments/8/edit
没有ID,URL帮助程序将无法建立正确的路由:
/comments/???/edit  # would raise an error because there are no route configured for this
请注意,此资源不必一定是相关的,但是在任何 View /页面(.html.erb)中,您通过url帮助器(对于link_to,form_with,redirect_to等)呈现url时,路由都必须正确形成。

关于ruby-on-rails - Rails验证或可能的路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64144792/

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