rails if statement refactor - ruby-on-rails-3

How can I wrap this whole block into an if statement so it only appears if there is at least one #event.speakers.show_in_meet_the_speakers_carousel present?
<div class="sectionTitle speakersColor">Meet the Speakers</div>
<div id="meetTheSpeaker">
<% #event.speakers.each do |speaker| %>
<% if speaker.show_in_meet_the_speakers_carousel.present? %>
<div class="item">
<img data-src="<%= speaker.featured_image %>" alt="<%= speaker.featured_image %>" class="lazyOwl">
<div class="speaker-info-wrapper">
<p><%= speaker.featured_copy if speaker.featured_copy.present? %></p>
<h4><%= link_to "Read More", speaker.featured_url %></h4>
</div>
</div>
<% end %>
<% end %>
</div>

<% if #event.speakers.select { |s| s.show_in_meet_the_speakers_carousel.present? }.count > 0 %>
<div class="sectionTitle speakersColor">Meet the Speakers</div>
<div id="meetTheSpeaker">
<% #event.speakers.each do |speaker| %>
<% if speaker.show_in_meet_the_speakers_carousel.present? %>
<div class="item">
<img data-src="<%= speaker.featured_image %>" alt="<%= speaker.featured_image %>" class="lazyOwl">
<div class="speaker-info-wrapper">
<p><%= speaker.featured_copy if speaker.featured_copy.present? %></p>
<h4><%= link_to "Read More", speaker.featured_url %></h4>
</div>
</div>
<% end %>
<% end %>
</div>
<% end %>

Related

How to setup Prismjs inside rich_text_area (Rails 7)

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.

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:

Why can I create a model with an hstore attribute, but get a 500 error when I try to update?

Borrowing heavily from:
https://github.com/heroku/hstore_example
I can define any number of "params" (my hstore column) from the New view when creating a new model and the hstore column gets set properly and the model is saved. Perfect!!
However, when I try to edit and submit I'm getting:
expected Hash (got String) for param `id'
Where 'id' is the name I gave to the hstore key of the "param" that I created.
Also interesting...I can Create AND Update the SAME MODEL from the console no problem.
WTF?!?
Here's my model:
class Testmethod < ActiveRecord::Base
attr_accessible :name, :params
serialize :params, ActiveRecord::Coders::Hstore
def as_json(options = {})
{ :name => name, :params => params }
end
end
Here's the form:
<%= form_for(#testmethod) do |f| %>
<% if #testmethod.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#testmethod.errors.count, "error") %> prohibited this testmethod from being saved:</h2>
<ul>
<% #testmethod.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :script %>
<%= f.text_area :script, :rows => 6 %>
</div>
<h4>Parameters</h4>
<div class="well">
<div class='clearfix attributeContainer'>
<div class='row'>
<p class='span3 underline'>Name</p>
<p class='span3 underline'>Value</p>
</div>
<% #testmethod.params.try(:each) do |key, value| %>
<div class="row">
<p class='span3'>
<%= text_field_tag key, key, :class => 'text_field dynamicAttributeName' %>
</p>
<p class='span3'>
<%= text_field key, :class => 'text_field', :value => value %>
</p>
<p class='span1'>
<a herf='#' class='btn removeRow'>X</a>
</p>
</div>
<% end %>
<div class='row attributeTemplate <%= 'hide' if #testmethod.params.present? %>'>
<p class='span3'>
<input class='text_field dynamicAttributeName' id='' name='' placeholder='New Attribute name' size='30' type='text' />
</p>
<p class='span3'><input class='text_field' id='bar' name='' placeholder='value' size='30' type='text' /></p>
<p class='span1'><a herf='#' class='btn removeRow'>X</a></p>
</div>
<%= link_to 'Add Parameter', '#', :class => 'btn addAttribute btn-mini' %>
</div>
</div>
<div class="row">
<div class="span12">
<div class="actions">
<%= f.submit "Save", :class => 'btn btn-primary ' %> or <%= link_to "Cancel", request.referrer || testmethods_path %>
</div>
</div>
</div>
<% end %>
<%= content_for :js do %>
<script type='text/javascript'>
$('.attributeContainer').delegate('.dynamicAttributeName', 'keyup', function(event){
nameElem = $(this);
valueElem = nameElem.closest('.row').children('p').children('.text_field')
value = nameElem.val().toLowerCase();
valueElem.attr('id', 'testmethod_params_' + value );
valueElem.attr('name', 'testmethod[params][' + value + ']');
valueElem.attr('placeholder', 'value for ' + value );
})
$('.attributeContainer').delegate('.removeRow', 'click', function(){
console.log($(this).closest('.row'))
$(this).closest('.row').html('');
})
$('.addAttribute').on('click', function(){
contents = "<div class='row'>" + $('.attributeTemplate').html() + '</div>';
$(this).before(contents);
})
</script>
<% end %>

How to hide a pages div if an attribute is blank on a page?

On my show page from a scaffold I have the following design:
<div id="show_location_main_area" class="grid_24">
<p>
<strong><%= #business_location.website %></strong>
</p>
<p>
<strong><%= #business_location.address %></strong>
</p>
</div>
<div class="grid_5">
<%= gmaps(#json) %>
</div>
If #business_location.address is blank I don't want to show this part on the page:
<div class="grid_5">
<%= gmaps(#json) %>
</div>
How is this done?
Thank you.
You can try following conditions -
unless #business_location.address.nil?
unless #business_location.address.empty?
if #business_location.address
Example -
<% unless #business_location.address.nil? %>
<div class="grid_5">
<%= gmaps(#json) %>
</div>
<% end %>
Just don't include it at all:
<% if #business_location.address.present? %>
<div class="grid5">
<%= gmaps(#json) %>
</div>
<% end %>
Or, if you need the .grid5 <div> there for your HTML/CSS to work:
<div class="grid5">
<% if #business_location.address.present? %>
<%= gmaps(#json) %>
<% end %>
</div>

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>