繁体   English   中英

Rails意外出现语法错误')'

[英]Syntax Error with rails unexpected ')'

嗨,我想在post#show视图中添加一个destroy动作,我想我已经创建了环境,但是当我将此代码放置在条件中时,出现消息错误。

<% if @user.comment == current_user %> 
  <% link_to @post_comment_path(post_id: @post.id, id: comment.id), method:    :delete, data: { confirm: "Are you sure?" } do %> 
  <i class="fa fa-trash"></i> 
<% end %>
 <% end %>

我在show#view后创建了一个名为_comments.html.erb的局部视图

这里是

<p class="text-center">Poster un commentaire</p>
      <%= simple_form_for [post, post.comments.new] do |f| %>
        <%= f.error_notification %>
        <%= f.input :content, label: "Commentaire"%>
        <%= f.submit "Envoyer", class: "btn btn-primary" %>
      <% end %>

它像这样<%= render 'comments' %>

在局部(在show#view之后)上方,我做了一个迭代

<ul class="list-unstyled">
    <% @post.comments.each do |comment| %>
   <li>
      <p><% comment.content %></p>
   <% end %>
   </li>
</ul>

但是当我创建新消息时什么也没出现,我不明白为什么。

我给你更多的代码细节

post.rb

has_many :comments, dependent: :destroy

comment.rb

belongs_to :user
belongs_to :post

路线是:

resources :posts do
  resources :categories
  resources :comments
end

评论控制器为

class CommentsController < ApplicationController

before_action :set_post

def create
  @comment = @post.comments.build(comment_params)
  @comment.user_id = current_user.id

  if @comment.save
    flash[:success] = "You commented the hell out of that post!"
    redirect_to :back
  else
    flash[:alert] = "There is a problem with your comment"
    render root_path
  end
end

def destroy
  @comment = @post.comments.find(params[:id])

  @comment.destroy
  flash[:success] = "Comment deleted :("
  redirect_to root_path
end

private

def set_post
  @post = Post.find(params[:post_id])
end

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

非常感谢你的帮助。

您的link_to erb代码需要等号才能实际显示

<%= link_to post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM