Am trying to display a 'total comments' number (i.e.7) count beside the article on the index page and not on the article page. Would like to use a ruby method for this as its probably the most straight forward...?
views/articles/_article.html.erb
<div class="article_header">
<b>Title: </b> <%= truncate(article.title, :length => 50) %>
by <%= article.user.username %> on <%= article.created_at.strftime("%d %B, %Y") %>
<b>Detail:</b> <%= truncate(article.body, :length => 225) %>
</div>
<br />
<%= blog.comments.count %>
<%= link_to 'Read', article %>
<% if can? :update, article %>
| <%= link_to 'Edit', edit_article_path(article) %> |
<% end %>
Pass in a variable when you call your partial:
= render "article", :display_count => true
Then in your partial:
<% display_count ||= false %>
<%= display_count ? blog.comments.count : '' %>
The correct way to do this would be:
<td><%= link_to "Comment count = #{article.comments.count}", article_path(article) %>
This will simply add another column to the output on your index page. It doesn't have to be linked if you simply want to display the count:
<td><%= "Comment count = #{article.comments.count}" %>
Related
The following index code works fine:
<ul id="sortable1" class="connectedSortable" data-update-url="<%= sort_tasks_url %>">
<% current_user.tasks.each do |task| %>
<%= content_tag_for :li, task do %>
<%= task.taskstatus.statuscode %>
<%= " - " %>
<%= link_to h(task.taskname), task %>
<%= " - " %>
<%= task.taskdesc %>
<% end %>
<% end %>
But, I can't figure out how to add a class to each li line. I get a syntax error with this:
<%= content_tag_for(:li, :class => 'ui-state-highlight'), task do %>
Thanks for the help!
Figured it out:
<%= content_tag_for( :li, task, :class => "ui-state-highlight") do %>
I now have a simple search form that will search one column in my model and return results as expected, however I would like to build upon this by being able to search upon 4 columns within the same model, using 4 text_field/Select_tags.
I have looked at Gems that provide this like solr for example but my thinking is that it seems a bit overkill for what i want to achieve, especially on such a small app
the app is simple, you can upload recipes, add recipes to favourites and search recipes.
So far it looks like this
Controller
def search
#countrysearch = Recipe.where(:country_of_origin => params[:search]).all
end
Search Form
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>
Output (View)
<% #countrysearch.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
<td><%= r.country_of_origin %></td>
<td><%= r.difficulty %></td>
<td><%= r.preperation_time %></td>
<td><%= ingredient_names(r.ingredients) %></td>
<td><%= preperation_steps(r.preperations) %></td>
</tr>
I would like my search form to mirror my "Create New recipe Form" with select tags
<%= f.label :dish_name, "Dish Name" %>
<%= f.text_field :dish_name, :placeholder => "Enter Dish Name" %>
<%= f.label :country_of_origin, "Country Of Origin" %>
<%= f.select :country_of_origin, [['Wales'],['Scotland'],['England']], {:include_blank => 'Please Select'} %>
<%= f.label :difficulty, "Difficulty Level" %>
<%= f.select :difficulty, [['Beginner'],['Intermediate'],['Expert']], {:include_blank => 'Please Select'} %>
<%= f.select :preperation_time, [['15..30'],['30..60'],['60..120']], {:include_blank => 'Please Select'} %>
<% end %>
Just some pointers in the right direction would be appreciated. Thank you
You can do it using all the parameters you have passed to controller:
def search
#countrysearch = Recipe.where({:dish_name => params[:search][:dish_name], :country_of_origin => params[:search][:country_of_origin], :difficulty => params[:search][:difficulty], :preperation_time => params[:search][:preperation_time]}).all
end
This should work for you. There are other alternatives, but if you want a simple one, this may fit you.
Edit:
If you don't have other things in your search for, you can do something like this:
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :dish_name, params[:dish_name] %>
<%= select_tag "country_of_origin", options_from_collection_for_select(#recipes, "country_of_origin", "country_of_origin_name")
<%= select_tag "difficulty ", options_from_collection_for_select(#recipe, "difficulty ", "difficulty ")
<%= select_tag "preperation_time", options_from_collection_for_select(#recipe, "preperation_time", "preperation_time")
<%= submit_tag "Search" %>
<% end %>
Then in the controlelr just pass the parameters directly
def search
#countrysearch = Recipe.where(params).all
end
right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)
I have a controller called votes_controller.rb. In that file there is the following action:
class VotesController < ApplicationController
def vote_up
#post = Post.find(params[:post_id])
vote_attr = params[:vote].merge :user_id => current_user.id, :polarity => 1
#vote = #post.votes.create(vote_attr)
end
(etc...)
I want to trigger the vote_up action in a view:
views/posts/show.html.erb::
<%= link_to "Vote Up", ??? %>
Here is the whole file just in case:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<%= link_to "Vote Up", ??? %>
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<% end %>
<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 have no idea what to type in the ??? part (I would also like to make it work as :remote. My intention is to trigger the action without refreshing the page).
Do I have to add something in the routes.rb?
Any suggestions?
You have to define a route in routes.rb. Use a named route to be easy to use in the view. Something like:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
And so can now use in the view
<%= link_to "Vote Up", vote_up_path(#post) %>
and in the controller
def vote_up
#post = Post.find(params[:id])
...
end
See Rails routing
I have a rails3 form that allows the user to edit a list of answers, as part of an assessment.
I use a fields_for loop to generate each text input:
app/models/assessment.rb :
class Assessment < ActiveRecord::Base
serialize :answers, Hash # answers is a t.text field used to store all answers.
end
app/view/assessments/new.html.erb :
<p>Initialized answers: <%= #assessment.answers %></p>
<% item_counter = 0 %>
<% form.fields_for :answers do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% #assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => #assessment.answers[item_id] %>
</div>
<% end %>
PROBLEM: The fields_for loop does zero iteration, no field gets printed.
( despite "Initialized answers:" showing correctly: {"a"=>143, "b"=>42} )
This should do. Tested locally.
<p>Initialized answers: <%= #assessment.answers %></p>
<% #assessment.answers.each do |key, value| %>
<%= form.fields_for :answers, #assessment.answers[key] do |answer_fields| %>
<div class="field">
<%= answer_fields.label key %>
<br/>
<%= answer_fields.text_field key, :value => value %>
</div>
<% end %>
<% end %>
Turns Hash to OpenStruct object solved my problem.
<% form.fields_for :answers, OpenStruct.new(answers) do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% #assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => #assessment.answers[item_id] %>
</div>