How to show maximin 250 symbols? For post.description
<% #microposts.each do |post| %>
<% a = post.had %>
<% b = post.needs.to_f %>
<% count = (a/b)*100 %>
<div class="col-md-12 post card-1 card-3">
<h3 class="pad-h3"><%= post.title %> <text class="needs"> Needs <%= post.needs %>$</text></h3>
<div class="row pad">
<div class="col-md-6"><a href="/help/id<%= post.id %>"><img class="img2" src="<%= post.image1 %>">
<h3 class="fun"><%= count %>% Funded</h3>
</a></div>
<div class="col-md-6"><p><%= post.description %></p></div>
</div>
<div class="row">Show more information</div>
</div>
<% end %>
Sometext SometextSometextSometextSometext Sometextv Sometextv vvvvvvSometextSometextSometextSometextSometextSometextSometextSometextSometextSometextSometextSome textSometextS dometext SometextSometextSometextSomete xtSometextSometextSometextSometextSometextSometext
Rails has truncate text helper:
<%= truncate post.description, length: 250 %>
Related
I need show a list of products in a carousel using bootstrap grouped by 4 elements in each 'item' class.
here is my develop
<div class="carousel-inner" role="listbox">
<% products.each_with_index do |product, n| %>
<% if n % 4 == 0 %>
<div class="item <%= 'active' if n == 0 %>">
<% end %>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
...
</div>
<% if n % 4 != 0 %>
</div>
<% end %>
<%end%>
</div>
the problem is when i try close 'item' class. I can't catch the opposite of multiples of 4.
<div class="carousel-inner" role="listbox">
<% products.each_slice(4).with_index do |slice, index| %>
<div class="item <%= 'active' if index == 0 %>">
<% slice.each do |product| %>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
...
</div>
<% end %>
</div>
<% end %>
</div>
I'm new to Rails so my code is probably ugly..
I have a loop which give me the 3 latest articles on my index page.
I added a condition to say "if there is no image in my article, give me a image from unsplash".
The issue is : that's apply the condition on the 3 images even if 2 of 3 images exists. I want that only the image which is nil get the unsplash image.
How can I do that ?
<% #articles.last(3).in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |article| %>
<% if article.image.exists? %>
<div class="col-xs-12 col-sm-4">
<div class="card">
<style media>
.card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("<%= article.image.url(:medium) %>"); }
</style>
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.subtitle, length: 45, escape: false) %></p>
</div>
</div>
</div>
<% else %>
<div class="col-xs-12 col-sm-4">
<div class="card">
<style media>
.card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("http://unsplash.it/1280/500/?random"); }
</style>
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.subtitle, length: 45, escape: false) %></p>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<% end %>
my controller :
class PagesController < ApplicationController
def index
#articles = Article.last(3)
end
end
Thanks a lot for your help !
I finally find the error.
It was <style media> which was the issue.
I finally do this :
<% #articles.last(3).in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |article| %>
<div class="col-xs-12 col-sm-4">
<a href="<%= articles_path(article.slug) %>"><div style="background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url('<%= article.image.url(:thumb) %>');" class="card">
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.soustitre, length: 45, escape: false) %></p>
</div>
</div>
</div></a>
<% end %>
</div>
<% end %>
Now the 3 latest articles are displayed on my index page.
I add a default image with paperclip and withdrawed the condition if else.
Display only the first 50 characters <%= news.header %>
view:
<% #news.each do |news| %>
<div class="news">
<div class="title"> <%= news.title %> </div>
<div class="photo"><%= image_tag( "1.jpg", :width =>247, :crop => :fit) %> </div>
<div class="header"> <%= news.header %></div>
<div class="cl"></div>
</div>
<% end %>
controller:
def index
#news=News.all
end
<%= news.header.truncate(50) %>
See http://api.rubyonrails.org/v4.2.7/classes/String.html#method-i-truncate
so I've created a web app that has user profiles, where users can search for fellow users based on interests etc.. How might I add a feature where users can favorite a persons profile? I.e. User A finds User B and likes what they see, and can click a 'Favorite this profile' button and perhaps it's starred, and stored? What would the code look like for this? Just an idea, but I'm open to all ideas.
user_profile.html.erb
<%= render "shared/header" %>
<div id="landing_welcome_page">
<div class="container">
<div class="row">
<%#= Profile image upload %>
<div class="span4">
<%= user_avatar(current_user) %>
<%#= Space w line %>
<div class="name"></div><br>
<%#= Please bare in mind these are strickly temporary placeholders i.e whitespace %>
<%= render 'social' %>
</div>
<div class="span8">
<%# User name %>
<span class="name1">
<% if current_user.first_name.blank? %>
<%= current_user.first_name.present? ? current_user.first_name : link_to('Finish your profile', edit_account_path)%>
<% else %>
<%= current_user.first_name %> <%= current_user.last_name %>
<% end %>
</span>
<span class="side-buttons">
<div class="name"></div>
</span>
</span>
</div>
<div class="row">
<br />
<div class="span6">
<%# User occupation %>
<i class="fa fa-usd"></i>:
<%= best_in_place current_user, :occupation, nil: 'Add occupation' %>
</div>
<div class="addy">
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>
</div>
</div>
<div class="span6">
<%# User gender %>
<br />
<% if current_user.gender == "M" || "male" %>
<i class="fa fa-male"></i> Male
<% else %>
<i class="fa fa-female"></i> Female
<% end %>
</div>
<div class="span2">
<!-- Code to calculate age by user birthday -->
<br />
Age: <%= user_birthday %>
</div>
<div class="span8"></div>
<div class="span8"><div class="name"></div></div>
<div class="span8">
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active">About me</li>
<li>Photos</li>
<li>Personality</li>
</ul>
<div class="tab-content">
<div class="tab-pane in active" id="tab1">
<% #questions_for_about.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
<div class="tab-pane" id="tab2">
<div class="page-header">
<%= form_for Photo.new do |f| %>
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add photos...</span>
<%= f.file_field :file, multiple: true, name: "photo[file]" %>
</span>
<% end %>
<div class="clearfix"></div>
</div>
<div class="photos_cont">
<div class="col-sm-6 col-md-3">
<span class="gallery"><%= render current_user.photos %></span>
</div>
</div>
</div>
<div class="tab-pane" id="tab3">
<% #questions_for_personality.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= render '/shared/footer' %>
</div>
</div>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%;"></div></div>
</div>
</script>
<script>
var fb_param = {};
fb_param.pixel_id = '6009056882201';
fb_param.value = '0.00';
(function(){
var fpw = document.createElement('script');
fpw.async = true;
fpw.src = '//connect.facebook.net/en_US/fp.js';
var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(fpw, ref);
})();
</script>
<noscript>
<img height="1" src="https://www.facebook.com/offsite_event.php?id=6009056882201&value=0" style="display:none;" width="1"/>
</noscript>
<script type="text/javascript">
// remove default datepicker event
jQuery(document).off('best_in_place:datepicker');
jQuery(document).on('best_in_place:datepicker', function(event, bip, element) {
// Display the jQuery UI datepicker popup
jQuery(element).find('input')
.datepicker({
format: element.data('date-format')
})
.on('hide', function(){
bip.update();
})
.on('changeDate', function(){
$(this).datepicker('hide');
})
.datepicker('show');
});
</script>
Create a Favorite model with a has_and_belongs_to_many relationship with User
Once you create the Favorite model and set up the relationship, you can do things like:
#user.favorites = [ user1, user2, user3 ]
#user.favorites << user4
to assign favorites to a user, or display them with something like:
<%= #user.favorites.map(&:name).to_sentence %>
You'll find everything you need to know on how to do this here: http://guides.rubyonrails.org/association_basics.html
so I've created a web app that has user profiles, where users can search for fellow users based on interests etc.. However sometimes the users don't fill out certain areas of their profile, i.e. about me may be left blank.. So how can I add the ability for users to only see the content of the profile that they filled out. In other words they can’t see the parts of other user profiles for which they have not completed.
user_profile.html.erb
<%= render "shared/header" %>
<div id="landing_welcome_page">
<div class="container">
<div class="row">
<%#= Profile image upload %>
<div class="span4">
<%= user_avatar(current_user) %>
<%#= Space w line %>
<div class="name"></div><br>
<%#= Please bare in mind these are strickly temporary placeholders i.e whitespace %>
<%= render 'social' %>
</div>
<div class="span8">
<%# User name %>
<span class="name1">
<% if current_user.first_name.blank? %>
<%= current_user.first_name.present? ? current_user.first_name : link_to('Finish your profile', edit_account_path)%>
<% else %>
<%= current_user.first_name %> <%= current_user.last_name %>
<% end %>
</span>
<span class="side-buttons">
<div class="name"></div>
</span>
</span>
</div>
<div class="row">
<br />
<div class="span6">
<%# User occupation %>
<i class="fa fa-usd"></i>:
<%= best_in_place current_user, :occupation, nil: 'Add occupation' %>
</div>
<div class="addy">
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>
</div>
</div>
<div class="span6">
<%# User gender %>
<br />
<% if current_user.gender == "M" || "male" %>
<i class="fa fa-male"></i> Male
<% else %>
<i class="fa fa-female"></i> Female
<% end %>
</div>
<div class="span2">
<!-- Code to calculate age by user birthday -->
<br />
Age: <%= user_birthday %>
</div>
<div class="span8"></div>
<div class="span8"><div class="name"></div></div>
<div class="span8">
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active">About me</li>
<li>Photos</li>
<li>Personality</li>
</ul>
<div class="tab-content">
<div class="tab-pane in active" id="tab1">
<% #questions_for_about.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
<div class="tab-pane" id="tab2">
<div class="page-header">
<%= form_for Photo.new do |f| %>
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add photos...</span>
<%= f.file_field :file, multiple: true, name: "photo[file]" %>
</span>
<% end %>
<div class="clearfix"></div>
</div>
<div class="photos_cont">
<div class="col-sm-6 col-md-3">
<span class="gallery"><%= render current_user.photos %></span>
</div>
</div>
</div>
<div class="tab-pane" id="tab3">
<% #questions_for_personality.each_with_index do |question, index| %>
<div class="question">
<h4 class="user_questions">
<%= index + 1 %>. <%= question.question %>
<%= link_to ("<i class='icon-edit'></i>".html_safe),
edit_user_question_path(current_user, question),
remote: true, class: "edit_link_#{question.id}" %>
</h4>
<div class="answer" id="answer_<%= question.id %>">
<%= answer_for(question) %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= render '/shared/footer' %>
</div>
</div>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%;"></div></div>
</div>
</script>
<script>
var fb_param = {};
fb_param.pixel_id = '6009056882201';
fb_param.value = '0.00';
(function(){
var fpw = document.createElement('script');
fpw.async = true;
fpw.src = '//connect.facebook.net/en_US/fp.js';
var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(fpw, ref);
})();
</script>
<noscript>
<img height="1" src="https://www.facebook.com/offsite_event.php?id=6009056882201&value=0" style="display:none;" width="1"/>
</noscript>
<script type="text/javascript">
// remove default datepicker event
jQuery(document).off('best_in_place:datepicker');
jQuery(document).on('best_in_place:datepicker', function(event, bip, element) {
// Display the jQuery UI datepicker popup
jQuery(element).find('input')
.datepicker({
format: element.data('date-format')
})
.on('hide', function(){
bip.update();
})
.on('changeDate', function(){
$(this).datepicker('hide');
})
.datepicker('show');
});
</script>
The conditionals that you have can be expanded to cover the whole div containing the info block.
For example:
<div class="addy">
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>
</div>
</div>
Could be
<div class="addy">
<% if current_user.address.present? %>
<div class="span2">
<%# User address %>
<i class="fa fa-home"></i>:
<%= current_user.address %>
</div>
<% else %>
<%= link_to('Add Address', edit_account_path) %>
<% end %>
</div>
Then it would only show the link if the address is not present.
Seems like you're on the right track. Basically, for every user attribute on the User model you want to show/hide, you'll want to add a conditional that evaluates whether or not the attribute on current_user is present?:
<span class="name1">
<% if current_user.first_name.blank? %>
<%= link_to('Finish your profile', edit_account_path) %>
<% else %>
<%= current_user.first_name %> <%= current_user.last_name %>
<% end %>
</span>
Alternatively, you can continue using ternary operators for single line evaluations, as you are already doing in some instances:
<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %>