RoR: Destroy option in form - ruby-on-rails

I have tried to put a dynamic field in my App to add new fields if the user need to put more information, but since i got no success im trying to get around, one that i maybe have found is a check box to distroy the field.
Well this is my view in _form.html.erb
<tbody name="bodao" class="jqueryform" id="bodao">
<tr class="tr" id="tr">
<td name="projetoid" class="projetoid" id="projetoid"> <%= f.grouped_collection_select :projeto_id, Cliente.order(:name), :projetos, :name, :id, :name, prompt: "Selecionar" %> </td>
<td name="raiz" class="raiz" id="raiz"> <%= f.number_field :seq%> </td>
<td name="seqpai" class="seqpai" id="seqpai"> <%= f.number_field :seqpai %> </td>
<td name="descr" class="descr" id="descr"> <%= f.text_field :descr %> </td>
<td name="hour" id="hour" class="hour"> <%= f.number_field :hour %> </td>
<td name="typo" id="typo" class="typo"> <%= f.select("typo", {"Analitica" => "Analitica", "Sintetica" => "Sintetica"}) %> </td>
<td><%= f.check_box :_destroy %></td>
</tr>
</tbody>
I dunno if this is possible, but i'm trying to put many of this code in my view and a check box where if marked the APP will ignore those fields and won't save in the database.
Testing it the way it looks, nothing works, cant POST due 500 internal server error.
Full _form code http://pastebin.com/c1CRQ2wJ
Firebug Post: http://pastebin.com/3KyT3EX1

Related

Conditionally calling 'collection_select' from a model/helper with AJAX

I'm looping through each instance of a built sub-tournament - and the problem that I'm having has to do with conditionally creating a collection_select box with data fetched via ajax. Here's the view - the line I want to insert code in is marked:
View
<% #tournament.sub_tournaments.each_with_index do |sub, i| %>
<%= f.fields_for :sub_tournaments, sub, :validate => false do |sub_form| %>
<div class="tab-content standings-row-<%= i %>" style="display:none">
<table>
<thead>
<tr>
<th> <h4>Standing</h4> </th>
<th class="standings-field-<%= i %>"></th>
<th></th>
</tr>
</thead>
<tbody>
<%= sub_form.fields_for :standings, :validate => false do |standings| %>
<tr>
<td>
<%= f.hidden_field :_destroy %><%= f.text_field :standing, :class => "standing", readonly: true, :type => "" %>
</td>
<td class="standings-ajax-<%= i %>">**INSERT HERE**</td>
<td><span class="remove">Remove</span></td>
</tr>
<% end %>
</tbody>
</table>
<div class="add-item">
<%= link_to_add_standings_fields(sub_form, :standings) %>
</div>
</div>
<% end %>
<% end %>
I thought about doing the conditional check (it depends upon whether the game selected is a team game or a single-player game) in the controller, but it seems to make more sense as a method (or a helper?). At the moment I have it in Standing.rb (below) - but I'm getting a no method "collection_select" error - so probably form helpers aren't available in models, which seems reasonable. So how could I do this?
Standing.rb
def team_or_player(game)
if Game::TEAM_GAMES.include?(game.name)
self.collection_select(:team_division_id, TeamDivision.where("game_id = ?", game.id),
:id, :name, {include_blank: true})
else
self.collection_select(:player_id, Player.where("game_id = ?", game.id),
:id, :handle, {include_blank: true})
end
end
And how can I pass the f to my AJAX call?
AJAX
$(".standings-ajax-<%= #tab_number %>").html("<%= ** ?? **.team_or_player(#standing, #game) %>");
You can call helper in model:
ActionController::Base.helpers.collection_select( ... )
So from what I can see you should change team_or_player() to class method and call it with:
Standings.team_or_player(#standing, #game)
or as instance
#standing.team_or_player(#standing, #game)
But that you should use self instead of passing #standing.
Me preference would be to put that logic directly in view or to helper.

How to Handle 2 tables with one form_tag? Rails 3

Beneficiaries
id (PK)
name
age
income
Beneficiary_loans
id (PK)
beneficiary_id (FK)
amount
rate
period
What I'm doing is, selecting a list of beneficiary from [Beneficiaries] table and displaying as follows
<%= form_tag({:action => 'update_survey_list_status', :status=>4}) do %>
<table width="100%">
<tr>
<th>Beneficiary Details</th>
<th>Amount</th>
<th>Rate</th>
<th>Period</th><th>
<input type='checkbox' name='checkall' onclick='checkedAll();'>
</th>
</tr>
<% #publishedlist.each do |b| %>
<tr>
<td><%= b.firstname %></td>
<%= fields_for :beneficiaryloan do |bloan| %>
<td> <%= bloan.text_field :amount%></td>
<td> <%= bloan.text_field :rate%></td>
<td> <%= bloan.text_field :period%></td>
<% end %>
<td><%= check_box_tag "benificiary_ids[]",b.id, :name => "benificiary_ids[]"%> </td>
</tr>
<% end %>
</table> <%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
In controller,
#beneficiaries=Beneficiary.find(:all, :conditions => ["id IN (?)", params[:benificiary_ids]])
#beneficiaries.each do |b|
#beneficiaryloan = Beneficiaryloan.new(params[:beneficiaryloan])
#beneficiaryloan.beneficiary_id=b.id
#beneficiaryloan.hfi_id=session[:id].to_s
#beneficiaryloan.status_id=params[:status]
#beneficiaryloan.save
end
What I'm not getting is
params[:beneficiaryloan]
Values are coming as NULL
Am I missing something here in this form?
Check the following,
With the help of any browser tool(eg:firebug), make sure that the form has been rendered properly. Sometimes due to some misplaced tags(generally happens with table structure) the form does not renders properly.
Try to remove the table structure and see if the form works.
Make sure your association is fine, i guess it should be Beneficiary has many Beneficiaryloan
If none of the above helps, please post the request parameters over here.

Rails Update Single Attribute with Multiple Fields

I'm currently trying to make a form called countcash that allows users to input the actual number of quarters, dimes, nickels, pennies, twenties, tens, fives, ones and then the monetary value of any other items in the cash box and then when they hit submit, I want to total those values and set the cashstart field of my current shift record.
The real issue is that I'm not sure what my form is supposed to look like and how the controller/model are supposed to be setup in this instance.
I have a shift_id saved in a session[:shift_id] variable and the shift with that id is the shift that I want to modify with the calculated cashstart.
What does the <%= form_for %> tag need to look like if I want to follow the MVC correctly and how do I set up the controller/model to update the database with the calculated value? And what should I be setting the route to? I can't find anything like this on either stack overflow or other rails forums (Although you'd think that modifying a single attribute through multiple text fields would be pretty straight forward)
Just as a reference, this is the form I was using previously (To no avail)
app/views/countcash.html.erb
<%= form_for #shift do |f| %>
<table>
<tr>
<th>Coins</th>
<th></th>
</tr>
<tr>
<td>Quarters: </td>
<td><%= f.text_field :quarters %><br /></td>
</tr>
<tr>
<td>Dimes: </td>
<td><%= f.text_field :dimes %><br /></td>
</tr>
<tr>
<td>Nickels: </td>
<td><%= f.text_field :nickels %><br /></td>
</tr>
<tr>
<td>Pennies: </td>
<td><%= f.text_field :pennies %><br /></td>
</tr>
<tr>
<th>Bills</th>
<th></th>
</tr>
<tr>
<td>Twenties: </td>
<td><%= f.text_field :twenties %><br /></td>
</tr>
<tr>
<td>Tens: </td>
<td><%= f.text_field :tens %><br /></td>
</tr>
<tr>
<td>Fives: </td>
<td><%= f.text_field :fives %><br /></td>
</tr>
<tr>
<td>Ones: </td>
<td><%= f.text_field :ones %><br /></td>
</tr>
<tr>
<th>Other</th>
<th></th>
</tr>
<tr>
<td>Value (in $): </td>
<td><%= f.text_field :others %><br /></td>
</tr>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And here's my shifts_controller that doesn't do anything at the moment, since I'm not sure how I need to go about updating the attribute after the submit button is clicked
def countcash
#shift = Shift.find(session[:shift_id])
redirect_to(login_path)
end
I think you are almost there, presumably you have a named route which allows you to do this as your form declaration:
<%= form_for #shift, :url => countcash_shift_path(#shift) do |f| %>
If in doubt, run rake routes in your console to determine if such routes exist. If it doesn't, perhaps you would want to explore RESTful routes, which should look something like this in your routes.rb:
resources :shifts
Inside the countcash action in your controller, it's just a matter of doing this:
#shift = Shift.find(session[:id])
#shift.update_attributes(params[:shift]
This should work assuming that you have you have set up RESTful routes for your shift resource. Also, I am assuming that your model/database is set up with quarters, dimes, etc as individual fields (otherwise you will require some more advanced than update_attributes).
Since you are not having these individual fields in the database (as per your comment), you should not be putting as part of the model's form. So instead of:
<%= f.text_field :dimes %>
You'd do something like:
<%= text_field_tag :dimes, params[:dimes] %>
And inside your controller, you can just access them like so:
params[:dimes] + params[:quarters] # multiply as per currency logic
If you want to get a little more fancy, what you could do is write methods that take in these values, so you still leave the form as your current state, but add methods that take in the values and do something with it like so (for all types of currency, not just dimes):
def dimes=(value)
#running_total += value
end
In your controller, do this instead:
#shift.attributes = params[:shift]
So what happens is that when you pass your model the hash of attributes, it will try to assign the values based on the symbol names. So the value of dimes in params will be passed in as if this was being called:
#shift.dimes = params[:shift][:dimes]
I think that should work for you, assuming that you use #running_total for something. You could use #amount or something if that's what you already have as a model attribute.

Problem passing params from view to controller in Ruby On Rails

I am new to ROR and am trying to pass two variables from two fields in a view to a controller to generate a dataset for an excel export. The excel export works, however all I get are headers and no data. I know it's because the parameters aren't being passed to the controller.
Here's the code. The SQL has been shortened...
In View
<table border="0" cellspacing="0" cellpadding="10">
<tr>
<td style="text-align: left; width:400px;"> Enter any part of a BU Manager Name, Subcontractor name, Subcontractor ID, PO Number, CRN or SCA name:</td>
<td style="text-align: left;">
<% form_tag :controller => 'subcontracts', :action => 'show_active_subcontracts_results', :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] , :id => 'search_field' %>
</p>
<td>
<p>
Expand Contract Period:
<%= select_tag 'c_end_date', options_for_select([["Current Subcontracts", "1"],["Past 5 Years", "2"], ["All", "3"]],{:mutiple => false}) %>
<%= submit_tag "Update", :name => nil %>
<%= link_to_function "Clear", "$('search_field').clear()" %>
<%= link_to 'Export To Excel',:action=>'active_subcontracts_to_excel', :format=> 'excel' %>
</p>
</td>
<% end %>
</td>
</tr>
</table>
What I want to do is pass the 'c_end_date' and 'search' data to the controller. Not sure if the link_to is the correct way.
Thank you for your help.
Your html is invalid: between the td above your form tag you're starting another td inside the form tag where the opening td hasn't been closed yet. This could be breaking your form. Try taking that td out entirely.

How to get the row numbers when I using render in RoR?

I use this code to generate the td tag format:
<%= render(:partial => "cart_item", :collection => #cart.items) %>
And this is the _cart_item.html.erb:
<tr>
<td class="column-left"><%=h cart_item.title %></td>
<td> x <%= cart_item.quantity %></td>
<td class="column-right">$ <%= cart_item.price %></td>
</tr>
But I want to know the row number as well. How can I do?
I believe you can use cart_item_counter, though I cannot find any documentation to back this up.

Resources