簡體   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