I have a view that has simple form inputs for several integers that I want to total - but I can't figure out how to do it. Obviously this doesn't work
OK to clarify this is a view form for the household model in haml (household doesn't have an # because I'm using decent exposure)
= simple_form_for household do |f|
%td= f.input :income1
%td= f.input :income2
%td= f.input :income3
%td Total Income #{household.income1.to_i + household.income2.to_i + household.income3.to_i}
I want to update the total as the user types in Income
How do I do get the total?
Ok I figured it out - this works but it doesn't display a total until it updates. I guess I need Ajax to do it live
Related
I have a user, who wants to be able to take vacation days off from work. My view looks like this:
<h2>Request Days Off</h2>
<%= form_for(#user, :as => :user, :url => vacation_days_path) do |f| %>
<div><%= f.label "How many vacation days would you like to take?" %>
<%= f.number_field :vacation_days %></div>
<div><%= f.submit "Submit" %></div>
<% end %>
In my controller, I have new and create methods. In all examples of the 'create' method I see on the internet, there is a line of code similar to
#person = User.new(user_params) or whatever
My issue is that I don't have a vacation_days model. Only a controller. I want to edit the User database, but creating a new user cannot be the answer (right?).
How do I create a working create method?
This is not really RESTful... However, if you want to update an existing user, you can do so like this
#user = User.find(params[:id])
#user.update_attributes(params[:user])
where params[:id] would hold the id of the user you want to update and params[:user] would hold the attributes you want to update.
Since you are using form_for(#user) with its form builder, it should be fine.
It doesn't seem to me that you need your separate controller for vacation days. Simply have a vacation_days/edit view, which contains your form, and have it submit to users/update.
For clarity, your action should be editing and updating your user, rather than 'creating' one. So, your controller action to update your user should have the line:
#user.update_attributes(params[:user])
I have 2 models user and team. a team can have many users each users belong to a team.
team_id is a column in my user table as a fk.
I want to have a select drop down on my create user form that has all my teams team_id in it.
This code does what I want BUT
<%= f.fields_for :teams do |builder| %>
<p>
<%= f.label :team_id, "Select Team For User" %>
<%= builder.collection_select :team_initials, Team.all, :id, :team_initials %>
</p>
<% end %>
it does not try to add the id value to a user. here is the error
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"xxxxx",
"user"=>{"name"=>"brand new",
"email"=>"test1#test.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"teams"=>{"team_initials"=>"27"}},
"commit"=>"Create my account"}
It complains that it cant add to the team model.
All I want to do is grab the ID of all existing teams for my create user form so when you crate a user you can select that users team.
The reason I have team_initials above is I was trying to use a more user friendly field for select but still user team Id as the value.
I think you're on the right track. The fields_for mechanism is for setting values in the associated model. I think all you're trying to do is make the association.
So you can lose the fields_for line and then this section:
builder.collection_select :team_initials, Team.all, :id, :team_initials
Should be:
f.collection_select :team_id, Team.all, :id, :team_initials
I'm looking here for my info on collection_select since I don't know it off the top of my head:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
I think this will change this parameter from "teams"=>{"team_initials"=>"27"}}, to "team_id"=>"27",
I hope that fixes it.
I am trying to build a dropdown select input of months using simple_form. However, I am having trouble figuring out where to even begin. Currently, it is a text input area:
<%= f.input :start_month %>
I need to know what arguments to pass in order for this to be a dropdown of all 12 months. It is not important for it to return an integer value for the months but would be ideal in the event I use it for ordering later on.
I am still a beginner with rails and could really use the help on this one. I can provide any extra information necessary.
Edit:
I would like the dropdown to show the month names, not simply numbers.
Here is my solution:
<%= f.input :start_month, collection: (1..12).map{|i| [I18n.t("date.month_names")[i], i]} %>
What about this:
<%= f.input :start_month, :collection => 1..12 %>
or with month names:
<%= f.input :start_month, :collection => ['January','February',...,'December'] %>
<%= f.select :month, Date::MONTHNAMES.compact.each_with_index.collect{|m,i| [m,i]}, prompt: 'Month'
I have a ruby on rails 3 project in which I query for a certain number of objects by using a .limit(3) . Then, in my view, I loop through these objects. After that, if there are 3 objects in the view, I display a "load more" button. Here is the view code:
<% #objects.each do |object| %>
<%= render object._type.pluralize.underscore + '/teaser', :object => object %>
<% end %>
<% if #objects.size(true) == 3 %>
#load more link here
<% end %>
The size(true) is passed a boolean to ensure that mongoID takes into account the .limit and .offset on my query (otherwise it returns the total number of objects that matched, regardless of the limit / offset). Here are the relevant development log lines:
MONGODB project_development['system.indexes'].insert([{:name=>"_public_id_1", :ns=>"project_development.objects", :key=>{"_public_id"=>1}, :unique=>true}])
MONGODB project_development['objects'].find({:deleted_at=>{"$exists"=>false}}).limit(3).sort([[:created_at, :desc]])
#some rendering of views
MONGODB project_development['system.indexes'].insert([{:name=>"_public_id_1", :ns=>"project_development.objects", :key=>{"_public_id"=>1}, :unique=>true}])
MONGODB project_development['$cmd'].find({"count"=>"objects", "query"=>{:deleted_at=>{"$exists"=>false}}, "limit"=>3, "fields"=>nil})
My question is: does MongoID do a separate query for my #objects.size(true)? I imagine the ['$cmd'] might indicate otherwise, but I'm not sure.
I don't think so, there was a pull request month ago to add aliases for :size, :length to :count to avoid re-running queries. You can check that.
I have an index where I'm showing a list of documents. I would like to implement a multiple select in order to do different actions to the documents the user has selected
I have created a
<%= check_box_tag 'id', 'document.id %>
for each document, inside a form_tag
But if I select multiple checkboxes, the params that are passed to the action are overwrited and I'm just receiving the id of the last checkbox I've selected in the id param.
¿Anyone knows how to implement multiple select?¿Any other approach?
I'm running Rails 3 and Ruby 1.8.7
Thanks in advance
You need to set :multiple => true
<%= check_box_tag 'id', document.id, :multitple => true %>
This will give you results in form of an array in params[:id]
Minor correction (plural):
<%= check_box_tag 'ids[]', document.id %>
ensure your model is properly set for attr_accessible something like :document_ids