Get id of an object to pass into form_for - ruby-on-rails

I am trying to find my time sheet id to pass into a form_for
here is my controller
def show
#time_id = current_user.time_sheets.find(params[:id])
if current_user
#current = current_user.time_sheets
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
This is my form_for in my view:
<%= form_for(:entry, :url => {:controller => Entry, :action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.hidden_field :time_sheet_id, value: #time_id.id %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
What needs to happen is that the timesheet id needs to get passed into the form_for so the entry can have the timesheet id.
I have user, timesheets and entries. user has_many time sheets, timesheets belongs to users and users have many time sheets. Entry belongs to timesheet.
I am getting this error "Couldn't find TimeSheet without an ID"

You should use "Nested Resources", eg. see here: Rails 3: How to create a new nested resource?
You could use form_for([#timesheet, #entry]) to pass the id of the timesheet without using a hidden field. If you do this, the :url param also become obsolete.

Try adding:
<%= #time_id.id %>
to output your variable and make sure it's exactly what you think it is. If #time_id is a timesheet, you should probably rename that variable to #time_sheet.

Related

Update function Ruby

I want this function, if the status of progress so that it changes on the done exactly at those IDs where the status is for the conditions, at the moment I get an error and secondly, if it worked without errors, then it would change everything, but I need if progress is finished and if you don’t, then on progress
I selected three records through the form using a checkbox ...
in the function, I want that if the status of the record from database is "done", then it should change to "progress", if the status is "progress" in the record, then it should be changed to "done". Now I click on "submit_tag" and I get an error and in any case this code will not work the way I want it, I want everything to be conditional.
I am completely in ruby, help please, maybe the problem is in the syntax
no implicit conversion from nil to integer
def update_me
#iteam = Iteam.find(params[:id])
if #iteam[params[:status]] == 'progress'
Iteam.where(params[:id]).update_all(status: 'DONE')
else
Iteam.where(params[:id]).update_all(status: 'progress')
end
end
index view
<%= form_tag update_me_iteams_path, :method =>'put' do %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Status</th>
<th></th>
</tr>
<% #iteams.each do |iteam| %>
<tr>
<td><%= iteam.id %></td>
<td><%= iteam.title %></td>
<td><%= iteam.text %></td>
<td><%= iteam.status %></td>
<td><%= link_to 'Show', iteam_path(iteam) %></td>
<td>
<%= check_box_tag "id[]", iteam.id %>
</td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
Please Use following to find with id:
Iteam.where(id: params[:id])
or you can use the following
Iteam.find(params[:id])

rails 5 form_tag with original DB value and after new selection in form, both records must be updated

I have a form_tag with a radio_button_tag and it populates with data from DB. It must simply be directed to a customised update action(update_multiple) where a boolean column is updated for all those 2 records which have been changed in the form.
For e.g. say when initially the form was populated from DB with record 1's radio button selected and now user changed his selection to record 3, then at Submit of form tag the update of both records must occur but the problem is the code at submit, only collects id of record which is now selected in that group.How do I get id of that record also which was unselected so that I can update_all for both of them at one go?
And if Submit cannot handle this action, then is there a way in controller or form to persist the id of the initial selected record before populating the form? As you see, I've tried with collecting an array of ids[] with radio_button_tag.
TIA for your help.
Here's the form code:
<%= form_tag update_multiple_user_cv_attachments_path, method: :put, action: :update_multiple do %>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th> Select a CV </th>
<th> Resume Name </th>
</tr>
</thead>
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<%= hidden_field_tag cv_attachment.main, :value => params[:main] %>
<tr>
<td><%= radio_button_tag "cv_attachment_ids[]", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= submit_tag "Select Main", :class =>'button' %>
<% end %>
Here's the controller update_multiple code.
def update_multiple
CvAttachment.update_all(["updated_at=?", Time.now], :id => params[:cv_attachment_ids])
end
I can think of 2 ways to achieve your objective.
update the boolean for all the attachments which belong to the user to false and then update the one which has been selected to true
include a hidden field in the form and set it to the id that is already true. Then in the controller action, update the one that is selected to true and the one in the hidden field to false. This is probably a better option and you'll probably want to wrap the d/b updates in a transaction.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main_cv", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "main_cv", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
controller
def update_main_attachment // probably a better name for this method
if params["ex_main_cv"] != params["main_cv"]
Attachment.transaction do
deselected_attachment = Attachment.find(params["ex_main_cv"]
deselected_attachment.update_attribute(:main, false)
selected_attachment = Attachment.find(params["main_cv"]
selected_attachment.update_attribute(:main, true)
end
end
end
Many thanks #margo. Here' how I resolved it partly your way of using hidden_field. But for now keeping this thread open as I'm making 2 DB updates for toggle of same column.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
and in controller:
def update_main
if request.put?
if params["ex_main"] != params["new_main"]
CvAttachment.find(params[:ex_main]).toggle!(:main)
CvAttachment.find(params[:new_main]).toggle!(:main)
end
end

Pass in ID from previous view/controller

I have been fighting this for a while.
I have a simple app that use users, timesheets and entries. timesheets belong to to users and an user has many time sheets. Entries belong to time sheets.
A user can click on a time sheet and be presented with the entries from that time sheet. I can create entries in the rails console and they show up correctly. But I am having trouble inserting the correct timesheet ID on the entries.
I may not be doing this correctly, but I have a link on the time sheet view to the entry view. I am having trouble bringing over the time sheet id to insert it in the the create entry view.
I am using a form_for that looks like this.
<%= form_for(:entry, :url => {:action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.text_field(:time_sheet_id, :value => #sheet_id ) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
This is my entry controller.
def create
#time_id = TimeSheet.find(1)
#new_entry = Entry.new(entry_params)
if #new_entry.save
flash[:notice] = "New Entry has been Added!"
redirect_to(:action => 'index')
else
render('new')
end
end
Do instead:
#new_entry = time_id.build_entry(entry_params)
Sorry, I Am closing this.
It was poorly worded and I figured out the problem.
All I needed to do is run the form_for from my timesheets controller and redirect it to the entries controller.

Editing Quantity in Cart

So this is another post about updating the quantity in the cart! Any one that I could find seemed to be outdated, so I apologize if this seems repetative.
But I am following along in Agile Web Development with Rails 4th edition book, and they were so kind as to leave editing the quantity as a 'challenge' and not show the answer :D. Now as I'm trying to get it to work I'm having troubles.
Show in my views/cart/show.html.erb I have the following table
<table>
<tr>
<th>Quantity</th>
<th>Product Name</th>
<th>Size</th>
<th>Price</th>
</tr>
<% #cart.line_items.each do |item| %>
<tr>
<td>
<%= form_for 'item', :url => {:controller => 'line_items', :action => 'update', id: item} do |f| %>
<div class="field">
<%= f.number_field :qty, :value => item.qty %>
<%= submit_tag "Update" %>
</div>
<% end %>
</td>
<td><%= item.product.name %></td>
<td><%= item.size %></td>
<td><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total Price</td>
<td><%= number_to_currency(#cart.total_price) %></td>
</tr>
</table>
Yet when I click update I get either
Unknown action
The action '29' could not be found for LineItemsController
or
Unknown action
The action '35' could not be found for LineItemsController
even if I completely take out the id field. I can deal with the update function on the controller side and getting it update properly - I want to figure that out on my own but I can't figure out what could possibly be generating these numeric actions and how I can fix it. In short, what is producing this error and how can I fix it? Is it perhaps related to the fact I have a line_item form in a cart view?
do you check the 29 and 35 is either ur id or anything else? try to check with your database for LineItems , and how your controller look like?? and
<%= form_for 'item', :url => {:controller => 'line_items', :action => 'update', id: item} do |f| %>
you trying to update it in ajax way or ? when update the quantity, should it be using ajax if it's not mistaken (the book asked to do in that way right? )
So I got it working - that I did was I tweaked the form header like so
<%= form_for :item, :url => line_items_update_path(id: item.id) do |f| %>
I added the following line to my routes.rb
get "line_items/update"
And added one line to my line_items_controller
def update
#line_item = LineItem.find(params[:id])
#line_item.qty = params[:item][:qty] #added this line here
For those who are having problems!

Rails forms helper: dealing with complex forms and params (it's my first day with rails)

Learning rails and something smells a little funny.
I have the following form for updating quantities in a shopping cart.
<% form_for(:cart, #cart.items, :url => { :action => "update_cart" }) do |f| %>
<table>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<% for item in #cart.items %>
<% f.fields_for item, :index => item.id do |item_form| %>
<tr>
<td><%=h item.title %></td>
<td><%=item_form.text_field :quantity, :size => 2 %>
<span># <%=h number_to_currency(item.item_price) %></span></td>
<td><%=h number_to_currency(item.line_price) %></td>
</tr>
<% end %>
<% end %>
<tr>
<td colspan="2">Total:</td>
<td><%=h number_to_currency(#cart.total_price) %></td>
</tr>
</table>
<%=submit_tag 'Update Cart' %>
<% end %>
In my update_cart action, I iterate through the existing cart items and set the new quantity:
def update_cart
#cart = find_cart
#cart.items.each do |item|
quantity = params[:cart][:cart_item][item.id.to_s][:quantity].to_i
#cart.update_quantity_for(item, quantity)
end
redirect_to :action => 'cart'
end
I dont have a REST interface controller for carts or cart items. Is there a better way to deal with this deep params data structure? The expression params[:cart][:cart_item][item.id.to_s][:quantity].to_i strikes me as dangerous for invalid form data.
The correct way to do this is the use the "accepts_nested_attributes" attribute in the cart model. Then you can just use CartController's update method to save your items. (http://railscasts.com/episodes/196-nested-model-form-part-1)
Also, your total price should probably be a method defined in the Cart model.

Resources