how can I pass an id of view for a controller?
My code:
Categoria View
<td><%= link_to 'Show', {:action => "show", :categoria_id => (params[:id]),:controller => "produtos", :method => "index"} %></td>
Produto Controller method: index
def index
#produtos = Produto.find(:all, :conditions => {:categoria_id => #categoria.id})
I want to get the id of the category and list all products with that id
As in your view you are passing :categoria_id => (params[:id]) , you can get that in controller using params[:categoria_id]
Moreover, the query for finding #produtos can simplifies as (For Rails 3+):
#produtos = Produto.where(:categoria_id => params[:categoria_id])
Related
I,m using bootstrap-datepicker-rails gem for date picker and want to shoe calender only on icon click(not on clicking text filed).
Write Now I'm using in my form:
= form_for (ServiceExp.new), :remote => true do |s|
%ul
%li
= s.label :position
= s.text_field :position
%li
= s.label :start_date
%p
From
%li
.input-append.date.datepicker
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon
To
.input-append.date.datepicker
= s.text_field :end_date, :class => "input-mini"
%span.add-on
.calander-icon
%li
= s.label :description
= s.text_area :description
= s.submit 'Save',:class => "btn btn-primary"
And in javascript:
$('.datepicker').datepicker();
Here, Calendar shows on both text field and icon because class datepicker on top of both. If use this class only on icon than it show calender only on icon click but not adding value on text field. How can I achieve that one. please give any suggestion !!
Add :"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013' # default date
To div container containing input box and span.
Like
%li
.input-append.date.datepicker{:"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013'} # default date
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon
I have the following as my f select
How do i modify to show what is in the database?
<%= f.select :phase_names, options_for_select([["Select One", "", #phase_names_string_value], "RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]), :class => 'inputboxes' %>
Phase Names is from a second table in the database.
however each project can only sit in one phase at a time.
Thanks in advance
options_for_select(container, selected = nil)
The container is display\value combination so you need [[value,name],[value,name]] or if its the same [name]
Like ["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]
Now that you have the container you need something to match the selected value
#current_value = MyModel.find(1).vari # Assume MyModel table with id has col vari=Completed
Then, you can do
select_tag "select_name", options_for_select(["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"].insert(0, "Select One"), #current_value)
Another way to do it is to have a collection of an object that holds the options with lets say :name (displayed) and :value (used as value) where selected has :phase_names = :value
f.collection_select :phase_names, [:name => "rStage", :value => "Pre Contract"] , :value, :name, {:include_blank => 'Select one'}
Which works just as activerecord classes
I'm sure there is a better way to do this, but in my inexperience, I'm having trouble sorting that better way out. I have a link that needs to send and object to controller for processing. It isn't working properly:
views/home/index.html.erb
<% search_term = "pizza" %>
<% #tag = Tag.find(:all, :conditions => ["name = ?", search_term ]).first %>
<li> <%= link_to(search_term, {:controller => "restaurants", :action => "index", :search_item => #tag}) %> </li>
controllers/restaurants.rb
def index
search_tag = params[:search_item]
#restaurants = Restaurant.search_by_tag(search_tag)
models/restaurant.rb
Class Restauarant < ActiveRecord::Base
def self.search_by_tag(search_tag)
search_condition = search_tag.name
#tags = Tag.find(:all, :conditions => [" name = ?", search_condition ])
#tag = #tags.first
#Restaurants = #tag.restaurants
end
end
This causes a NoMethodError in ResataurantsController#index
parameters:
{"searchitem" => "15"}
For some reason, the tag object isn't being passed properly from the home/index.html.erb and is only passing along the Tag-object :id to the restaurants controller. Isn't possible to pass a full object this way. what am I doing wrong?
You can't submit an object through get params like that. Typically, you'll just pass the object's id (which you are already doing), and then do a lookup in the controller:
#tag = Tag.find(params[:search_item])
It would make more sense to rename the "search_item" param to "tag_id".
I am finishing up my first RoR project, and am working on a leaderboard system that shows the number of points users have accrued for correctly answering quiz questions.
I am getting all of the users that have answered at least one question correct, grouping them by user_id, and displaying them in descending order by most correct using this:
#users = Point.find(:all,
:group => 'user_id',
:order => 'correct DESC', :conditions => { :correct => "yes"})
In my view, I am using this to iterate through the results:
<% #users.each_with_index do |user, index| %>
However, I am not able to get the number of correct answers per user. I tried:
user.count
but that doesn't work. How do I get the number of items per group?
You're on the right track. Seems like you would be better off using the all command with the count condition within it as opposed to the count command. Something like this:
Point.all(:select => 'user_id, count(id) as point_count', :group => :user_id, :conditions => { :correct => 'yes' }, :order => 'point_count desc', :limit => 10)
This will return 10 limited Point objects with a user_id attribute (so you can still access the user relationship), and a point_count attribute with the number of correct points said user has obtained.
Note: you could change the limit to be however many users you wanted to display in your leaderboard. This example would return 10.
It might make more sense to have your code look like this:
#points = Point.all(:select => 'user_id, count(id) as point_count', :group => :user_id, :conditions => { :correct => 'yes' }, :order => 'point_count desc', :limit => 10)
And as I said in a comment below, you could iterate through them by doing something like this (this would assume that your User model has a name attribute):
<table>
<% #points.each do |point| %>
<tr>
<td><%= point.user.name %></td>
<td><%= point.point_count %></td>
</tr>
<% end %>
</table>
I think the problem may be that you think you're getting an Array back, but you actually get a Hash back.
Try doing:
p #users
(which is equivalent to puts #users.inspect). You'll probably see it's more so something like:
{ "1" => [UserObject, UserObject], "2" => `[UserObject] }
You can even do p #users.class and you'll see it's not an array.
When you loop with a .each_with_index on a Hash, you need to do:
#users.each_with_index do |(key, value), index|
Then you can do #users[key].count or value.count.
Figured out how to get the correct count:
#users = Point.count(:group => :user_id, :conditions => { :correct => "yes"})
The most simple way should be:
#user.points.where(:correct => "yes").count
Though this will only work if have defined your associations in the user and point model like
class User < ActiveRecord::Base
has_many :points
class Point < ActiveRecord::Base
belongs_to :user
(personally I would have used a bool flag (smallint) instead of string for the "correct" column.
I have this bit of code and I get an empty object.
#results = PollRoles.find(
:all,
:select => 'option_id, count(*) count',
:group => 'option_id',
:conditions => ["poll_id = ?", #poll.id])
Is this the correct way of writing the query? I want a collection of records that have an option id and the number of times that option id is found in the PollRoles model.
EDIT: This is how I''m iterating through the results:
<% #results.each do |result| %>
<% #option = Option.find_by_id(result.option_id) %>
<%= #option.question %> <%= result.count %>
<% end %>
What do you get with this:
PollRoles.find(:all,:conditions=>["poll_id = ?",#poll.id]).collect{|p| p.option_id}
You want to use this function to do things like this
PollRoles.count(:all, :group => 'option_id') should return a hash mapping every option_id with the number of records that matched it.