Best Way to Add Comments in Erb

Best way to add comments in erb

Use the <%# %> sequence, e.g.

<%# This is a great comment! %>

How to comment lines in rails html.erb files?

ruby on rails notes has a very nice blogpost about commenting in erb-files

the short version is

to comment a single line use

<%# commented line %>

to comment a whole block use a if false to surrond your code like this

<% if false %>
code to comment
<% end %>

How does one comment in an erb template?

=begin and =end are the Ruby version of block comments.

Using them in an erb template:

<%
=begin
%>
<li class="someclass">
<=% t'model.attr' %>
</li>
<%
=end
%>

Block comments in html.erb templates in rails

I wouldn't count as a solution, but perhaps enclosing the chunk between an

<% if false %>
...
<% end %>

or if you feel a little dirty, create a helper that simply outputs nothing.

I've never needed it, but I'm stumbled there seems to be no out-of-the-box solution for this.

How do I comment out ERB in Rails?

<%#= link_to "Make default", make_default_admin_state_path(state) %>

<%# %> is an ERB comment: the dangling = makes no difference, and can be left in.

Why can't I get /comments/new.html.erb?

Its because you don't have just '/comments/new' path but form_for(@comment) is trying to create it.

Your routes are creating path like '/post/:id/comments/new' so you have to use form_for([@post, @comment]).

Also add @post = Post.find(params(:is)) in new method or better as before_action callback.

Correct way to add a comments to existing view rails

This is typically handled by nested resources:

#config/routes.rb
resources :images do #-> url.com/images/:id
resources :comments, only: [:create, :update, :destroy] #-> url.com/images/:image_id/comments
end

#app/controllers/images_controller.rb
class ImagesController < ApplicationController
def show
@image = Image.find params[:id]
@comment = @image.comments.new
end
end

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
@image = Image.find params[:image_id]
@comment = @image.comments.new comment_params
redirect_to @image if @comment.save
end

private

def comment_params
params.require(:comment).permit(:body).merge(user_id: current_user.id)
end
end

You'll be able to show the views as follows:

#app/views/images/show.html.erb
<%= @image.attribute %>
<%= form_for [@image, @comment] do |f| %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>

Of course, you could put this into a partial. There are a number of ways to get it working, the above is just the way I'd handle it.

comment.user.username not working on show.html.erb

I assume, in inquests/show.html.erb you're displaying multiple comments, something like

<%= @inquest.comments.each do |comment| %>
<%= comment.user.user_name %>
<%= comment.content %>
<% end %>

Many comments will render without issue. Comment model and database doesn't allow user_id to be nil.

But looks like one comment's user_id doesn't have a corresponding id in users table. When you try to figure out what's going on and remove user_name

<%= @inquest.comments.each do |comment| %>
<%= comment.user %>
<%= comment.content %>
<% end %>

Sneaky broken comment probably doesn't show you anything, comment.user is nil, and because you have no validation on comment.content it could also be nil.

First, get rid of comments without user to verify this is the issue:

# this is fast enough for a few thousand comments
>> Comment.find_each { |comment| comment.destroy unless comment.user }

After this inquests/show.html.erb should be working.

To make sure this doesn't happen again:

class User
# this will delete all `user.comments` when you destroy `user`
has_many :comments, dependent: :destroy

# ...
end

To really make sure this doesn't happen again:

class CreateComment < ActiveRecord::Migration[7.0]
def change
create_table :comments do |t|
t.references :user, null: false, foreign_key: true

# ...
end
end
end

With foreign_key constraint, your database will not let you destroy a user if they have comments. This works in tandem with dependent: :destroy. If you delete a user and rails automatically destroys all user.comments, then database will not complain.

Probably do the same for inquest as well if it's not optional.

Also comments without content are not really comments:

class Comment < ApplicationRecord
belongs_to :inquest
belongs_to :user

validates :content, presence: true
end


Related Topics



Leave a reply



Submit