How to setup Prismjs inside rich_text_area (Rails 7) - prismjs

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.

Related

Rails collection_select , and value automatically added in textfield

Hi i am currently using Collection_select like so , and as you can see its working properly but i want to make it, so that when i pick 1 item from the Item field that the data from my database , bought price and sold price would automatically be added into the textfields like in the example below.
but as to how i can do it, remains a mystery to me, can anybody help me with this and thanks! any help will be helpful! and thanks again!
the code that i used
<%= form_for(#sale) do |f| %>
<% if #sale.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale.errors.count, "error") %> prohibited this sale from being saved:</h2>
<ul>
<% #sale.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :item_id %><br>
<%= collection_select( :sale, :item_id, Item.all, :id, :name, {} ) %>
</div>
<div class="field">
<%= f.label :bought %><br>
<%= f.text_field :bought %>
</div>
<div class="field">
<%= f.label :sold %><br>
<%= f.text_field :sold %>
</div>
<div class="field">
<%= f.label :number %><br>
<%= f.number_field :number %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
you can do this by send ajax request and bought data from database then show "bought price" and "sold price" in there respective fields. Something like that
$("#collection_select_id").change(function () {
$.ajax({
url: "your_url_where_you_get_your_data",
method: "GET",
data: {},
success: function(data) {
$("#bought_price_id").val(data.bought_price)
$("#sold_price_id").val(data.sold_price)
}
});
});
Thanks

How to show file upload button once in a nested form in rails 4?

I have a parent (playlist) -> child (tracks) structure in my rails app.
What I am doing in the form is showing attributes from the parent and then I have a table for the child attributes.
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Playlist Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<h5> description: </h5>
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div>
<h5> Add some music: </h5>
</div>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="display">
<thead>
<tr>
<th>Delete</th>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
I am using paperclip to upload the files. I am uploading images on playlist model and mp3s on the tracks model.
The code in question is the following:
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
I feel this is a hack. Why do I have to add a new track for me to show the file_field (choose file button) once? Towards the bottom of the form, you will notice that I have to call
<%= f.fields_for :tracks do |ff| %>
again because I want to iterate through all the tracks for the playlist and show them in a table.
Not sure if this question makes sense, but how do I show the file_field once (I feel Track.new is a hack and I think its messing up in saving the model)?
EDIT:
here is a screenshot of what happens if I don't have Track.new:

Rails 3 & Devise: Error when logout

Problem solved
klaustopher his answer fixed the problem.
The problem is that i can't logout when i have <%= current_user.email %> or <%= current_user.username$> in my layouts/application.html.erb
When i delete this line out of my application.html.erb, i am able to logout without errors.
I've searched on google but could not find anything about this problem.
My application.html.erb:
<!DOCTYPE html>
<html id="html">
<head>
<title><%= content_for?(:title) ? yield(:title) : "Contractbeheersysteem" %></title>
<%= stylesheet_link_tag "application", "simple_form", "gegevens", "drop-down-menu", "table", :media => "all" %>
<%= javascript_include_tag "autocomplete-rails.js" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
<%= javascript_include_tag 'jquery.dataTables.min' %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
<body>
<body link="#999" vlink="black" alink="black">
<div id="menu">
<ul id="drop-down-menu">
<li><%= link_to "Home Page", home_index_path, :class => current_page?(home_index_path) ? "current" : "" %></li>
<li><%= link_to "Bedrijfsgegevens", bedrijfsgegevens_path, :id => 'bednav' %>
<ul>
<li><%= link_to "Nieuw Bedrijf toevoegen", new_bedrijfsgegeven_path %></li>
</ul>
</li>
<li><%= link_to "Contactpersonen", contactpersoons_path, :id => 'contanav' %>
<ul>
<li><%= link_to "Nieuw Contactpersoon", new_contactpersoon_path %></li>
</ul>
</li>
<li><%= link_to "Contractgegevens", contractgegevens_path(#contractgegevens), :id => 'contrnav' %>
<ul>
<li><%= link_to "Nieuwe Contractgegevens", new_contractgegeven_path %></li>
</ul>
<li><%= link_to "Contactformulier", contact_index_path, :id => 'formnav' %> </li>
<% if user_signed_in? %>
<li><%= link_to "My Account", users_path(#users) %>
<ul>
<li><%= link_to "Uitloggen", destroy_user_session_path, :method => :delete %>
<li><%= link_to "Gebruikers", user_registration_path %>
</ul>
<% else %>
<li><%= link_to "Inloggen", new_user_session_path %>
<ul>
<li><%= link_to "Registreren", new_user_registration_path %>
</ul>
<% end %>
</ul>
<p class="clear_all"></p>
</div>
<div id="login">
Inlogd als: <%= current_user.username %>
</div>
<div id="content">
<hr>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</div>
<div id="footer">
<hr color="#999", width="100%">
<h4>
<b>Piter Jelles © 2012 </b> | Ruby on Rails
</h4>
</div>
</font>
</body>
</div>
</html>
Note: i'm from the netherlands so there are dutch words in my application.html.erb
When you are logged out, current user will return nil. Calling a method on nil will return in an error
1.9.3p125 :002 > nil.username
NoMethodError: undefined method `username' for nil:NilClass
You have to check if current_user returns something other than nil. You can do this by using
<%= current_user.username if current_user %>
you need to check the existence of current_user like:
<% if current_user.present? %>
<%= current_user.email %> or <%= current_user.username %>
<% end %>

Hide 'Publish' Button From non Admins?

Am trying to hide this publish 'tick-box' from non-admin users. I used the CanCan plug-in and set up the correct permissions but am struggling with the code syntax. I have used <%= if can? :publish, article %> in the views/articles/_form.html.erb partial but it doesn't work?
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<div class="field">
<%= check_box("article", "published" ) %>
**<%= if can? :publish, #article %>**
<%= "Publish article" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
You should be using <%, not <%=. Also, the if statement is in the wrong place, and there's no closing end statement. Here's correct code:
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<% if can? :publish, #article %>
<div class="field">
<%= check_box("article", "published" ) %>
<%= "Publish article" %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>

Rails 3 - Forms - Jquery :remote make duplicates and prompts 2x?

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.