I am using ruby .erb to display time in a div's span. However using something similar for date does not work.
<div id="comment_space">
<% #post.comments.each_with_index do |comment,date,posted,index| %>
<span class="above"><%= date = ('%Y-%m-%d') %> </span>
<div class="comment" id="msg_<%= index+1 %>">
<%= comment.usr_comment %>
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
</div>
<% end %>
<span class="above">
<%= date = ('%Y-%m-%d') %>
</span>
This is the part not working. However
<span class="right">
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>
works perfectly.
The first error was that "created_at" was not lked for the date and it has gone down hill from there. The date and time were defined in Javascript with timestamps in a Ruby ActiveRecord table.
<%= date = ('%Y-%m-%d') %> # Ehhhhhm
Ok, compare that to your other way of doing this.
<%= comment.created_at.localtime.strftime("%l: %M %p") %>
See the difference?
If the "date" is Date class, you can do:
<%= date.strftime('%Y-%m-%d') %>
and it should work with no problem whatsoever.
If you do what you did, you say that the date is now '%Y-%m-%d'
date = ('%Y-%m-%d')
# => "%Y-%m-%d"
Related
I have a collection checkboxes in my form, like below.
<% ["cricket" ,"tennis", "not there in list"].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
Currently the label of the checkbox, and its value that is getting saved in the db, both are same. i want to save different values to the db for the labels. i tries like below. but its not working. can anyone help me with this.
<% [["cricket","cri"] ,["tennis","ten"], ["not there in list","na"]].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓
<% [["cricket","cri"], ["tennis","ten"], ...].each do |name, val| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(val)}, name, "" %> <%= name %></p>
</div>
<% end %>
I am trying to make a search bar in order to find some cocktails by their name in my index. All I got is an empty array...
I believe my SQl request is not good... I'd need help please to make it working. Regards
Here is my code:
Search form:
<%= simple_form_for :query, url: cocktails_path, :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit %>
<% end %>
cocktails controller:
def index
if params[:query].present?
#cocktails = Cocktail.all
#cocktail_search = #cocktails.where('cocktails.name LIKE ?', params[:query][:search])
binding.pry
else
#cocktails = Cocktail.all
end
end
the index view:
<% if #cocktail_search %>
<% #cocktail_search.each do |cocktail| %>
<div class="col-xs-12 col-sm-3">
<div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)),
url('<%= cl_image_path cocktail.photo %>');">
<h2 class="card-description"><%= link_to cocktail.name, cocktail_path(cocktail)%></h2>
</div>
</div>
<% end %>
<% else %>
<% #cocktails.each do |cocktail| %>
<div class="col-xs-12 col-sm-3">
<div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)),
url('<%= cl_image_path cocktail.photo %>');">
<h2 class="card-description"><%= link_to cocktail.name, cocktail_path(cocktail)%></h2>
</div>
</div>
<% end %>
<% end %>
This is my binding pry
#cocktail_search = #cocktails.where('cocktails.name LIKE ?', params[:query][:search])
If your table has column "name", then:
#cocktail_search = #cocktails.where('name LIKE ?', params[:query][:search])
should do the work.
When you're uncertain about what is wrong you can rails console and check things yourself, ex. Coctail.where("name like ?", "Mojito") and see if it returns expected result.
Since you use LIKE it seems like you want to find cocktails every if the search term matches only parts of the name. This can be archived by using % (see: Safe ActiveRecord like query).
Furthermore you might want to use a case-insensitive ILIKE instead of LIKE:
#cocktails.where('name ILIKE ?', "%#{params[:query][:search]}%")
Without the % you query is basically the same as
#cocktails.where(name: params[:query][:search])
that only returns exact matches.
I want people to be able to challenge their habits. If they miss a day they may put "/" if they miss two days in a row they must put "X". Three X's means they failed the challenge.
How can I force the User to only be able to submit X's and /'s as valid keys in the :missed input field?
_form excerpt
<%= simple_form_for(#habit) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :missed %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Github: https://github.com/RallyWithGalli/ruletoday
Thanks in advance for your help. You rock!
You can use a select tag to limit users' options like this
<%= select_tag(:missed , options_for_select([['/', '/'], ['X', 'X']])) %>
This is the last remaining item to complete my first rails app and need some help.
On each user profile (localhost:3000/users/username), there's a listing of posts that the user has made. Associated with each post are comments. So post_id: 3 could have comments.
I have it working already in view form but I need the comments to appear in a popup instead when the "Comments" link under each post is clicked.
I have already applied facebox which is a jQuery-based lightbox that displays popups.
I just need to move what's currently shown in show.html.erb into a popup.
There's the _comment_form.html.erb which renders into _post.html.erb
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
<div class ="ItemComments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(comment.user.email)}" %>
<span class="users"><%= link_to comment.user.name, comment.user %></span>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
The above renders into _post.html.erb using:
<%= render 'shared/comment_form', post: post if signed_in?%>
Then it renders into show.html.erb
I'm trying to use this line, but what do I link it to?
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
This is shared/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar") %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
One way of doing this is to render your comments into a hidden div and give that div an id. Next you point your link to the id of the div using # followed by the id. It would look something like this:
_post.html.erb
<%= link_to "#comments", :rel => "facebox" do %>
<%= post.comments.count.to_s %>
<% end %>
<div id="comments">
<%= render 'shared/comment_form', post: post if signed_in?%>
</div>
CSS
#comments {
display: none;
}
See the 'Divs' heading over at the Facebox docs.
What I want to do is make it just output the view itself, and ignore what Rails would normally think is embedded Ruby within the HTML.
For example:
<div class="current">
<div class="question">
<h3 id="homework_name"><%= homework.name %><h3 id="due">Due <%= homework.due %></h3></h3>
<h2 class="title">The Question:</h2>
<p id="question_contents"class="contents"><%= current_question.contents</p>
</div>
<div class="answer">
<h2 class="title">Your Answer:</h2>
<textarea class="contents" id="student_answer"><%= current_question.answer %></textarea>
</div>
</div>
I want an ActionView Template Handler to ignore all mentions of:
<%= homework.name %>
<%= homework.due %>
<%= current_question.contents %>
<%= current_question.answer %>
More specifically, it should ignore any tags starting with <% and ending with %>
If you need more info check out http://pastie.org/private/epsxnapldho6co2y0indg
Here you go https://gist.github.com/1144297
And use .html.lifo extensions for your templates.