Trying to figure out how to use Prismjs inside rich_text_area so I can use it for syntax highlighting in a blog with Rails 7.
_form.html.erb- currently working great as below
<%= form_with(model: blog) do |form| %>
<% if blog.errors.any? %>
<div style="color: red">
<h2><%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% blog.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :content, style: "display: block" %>
<%= form.rich_text_area :content %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
As you can see in both instances where I have input the code using the "code block" and by simply adding it to the "text area" the results are the same.
When I add the same code to any other page it works as expected.
This is the login page for my app but the role selected for a user is not being reflected in the database so i want to try changing form_for into form_tag to see if the data gets submitted.
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_tag users_path, methods=> post:do %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag 'email' %><br />
<%= text_field_tag :email, params[:email], :placeholder => "Email" %>
</div>
<div class="field">
<%= label_tag 'username' %><br />
<%= text_field_tag :username, params[:username], :placeholder => "Username" %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag 'password' %>
</div>
<div class="field">
<%= label_tag :password_confirmation %><br />
<%= f.password_field_tag 'password_confirmation' %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= submit_tag 'submit' %>
</div>
<% end %>
The following view shows a single post and its comments:
views/posts/show.html.erb:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<div class="post-<%= #post.id %>">
<h3><span class="vote-count"><%= #post.total_votes %></span> votes</h3><br />
<div class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
<%= link_to "Vote Up", vote_up_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-up" %><br />
<%= link_to "Vote Down", vote_down_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-down" %><br />
<h2>Comments</h2>
<p><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></p>
<p><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></p>
<% #comments.map do |comment| %>
<div class="comment-<%= comment.id %>">
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Vote:</b>
<span class="vote-count"><%= comment.total_votes %></span>
<div class='voted-user'>
<% comment.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<p>
<b>Link</b>
<%= link_to "Show Post Comment", [#post, comment] %>
</p>
<p>
<b>Vote</b>
<%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
</p>
</div>
<% end %>
<%= will_paginate #comments %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I just focused on making things work so I totally forgot to make it clean.
I'm a Rails beginner and I would like some suggestions or advises to clean this view
(if you suggest to move code to another file please mention the name of the file and the directoy). Thanks in advance.
Good on you for wanting to clean it up. This is some of what I would do. I've included some examples of a few things here: Partials, Helpers, and also cleaned up the HTML a little to allow more control over the style in your stylesheets (which I left out, but you can figure out that part I'm sure). If this was my project, I would extract everything even more (for example, I would probably move the entre "edit/back" links at the bottom, as well as the "Order" links, to their own partials or helpers, since I would probably be using them in a lot of places throughout the application.) And if your comments are polymorphic (i.e., not just for posts), then definitely move that into its own view file. Also, I did this a rather quickly, so it might have some errors, sorry if you find any.
_post.html.erb
<article class="post-info">
<span class="title"><b>Title:</b> <%= post.title %></span>
<p><%= post.content %></p>
<span class="user"><b>User:</b> <%= post.user.username %></span>
</article>
<div class="post" id="post-<%=post.id%>">
<div class="vote-count"><%= post.total_votes %></span> votes
<ul class="voted-user">
<% post.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
<ul class="voting">
<li><%= link_to "Vote Up", vote_up_path(post, :votable_type => "Post"), :remote => true, :class => "vote-up" %></li>
<li><%= link_to "Vote Down", vote_down_path(post, :votable_type => "Post"), :remote => true, :class => "vote-down" %></li>
</ul>
_comment.html.erb
<div class="comment" id="comment-<%=comment.id%>">
<article class="comment">
<b>Comment:</b> <%= comment.content %>
</article>
<div class="votes-total">
<b>Votes:</b> <span class="vote-count"><%= comment.total_votes %></span>
<ul class='voted-user'>
<% comment.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
</div>
<div class="commenter">
<b>Commenter:</b> <%= link_to comment.user.username, comment.user %>
</div>
<div class="link">
<b>Link:</b> <%= link_to "Show Post Comment", [comment.post, comment] %>
</div>
<div class="vote">
<b>Vote:</b> <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %>
</div>
</div> <!-- .comment -->
show.html.erb
<h2>Posts</h2>
<%= render #post %>
<h2>Comments</h2>
<ul class="order">
<li><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></li>
<li><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></li>
</ul>
<%= render #comments %>
<%= will_paginate #comments %>
<h2>Add a Comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<ul class="manage">
<li><%= edit_link_if_allowed(current_user, #post) %></li>
<li><%= link_to 'Back', posts_path %></li>
</ul>
Posts Helper
def edit_link_if_allowed(current_user, post)
link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
end
You could use Partials
So I have been trying to follow some paperclip tutorials and adjusting them to rails 3. I follow the steps and got an error once I started to add the code needed for the _form and show.htm.erb files. This is the error I get.
Error Message:
ActionView::Template::Error (undefined method `label' for nil:NilClass):
11: </div>
12: <% end %>
13: <div class="field">
14: <%= form.label :photo, "Photo" %>
15: <%= form.file_field :photo %>
16: </div>
17: <div class="field">
Form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<div>
<% end %>
<div class="field">
<%= form.label :photo, "Photo" %>
<%= form.file_field :photo %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>`
form.label doesn't make sense, since you're calling your form variable f inside your block, with the line form_for ... do |f|.
You need to use f.label etc.
Hi
Im working with a form and have added :remote => true to the form and the destroy link. But when I do that this happends:
I submit the form and it makes 2 versions that are the same.
I destroy, I get prompted twice?
If i remove the :remote => true it only makes one version and only prompt once.
This is my form
<%= form_for([#comment], :remote => true) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %>
<br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.hidden_field :user_id %>
<%= f.hidden_field :retailer_id %>
<%= f.hidden_field :product_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my destroy link
<%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete, :remote => true %>
Am I doing something very wrong here? Thanks in advance!
I think you have included your rails.js file twice. that might be the problem. check it.