I have this problem... I need save my table historic with all the authorizations ids checked, update the column authorization_origin_id and set the column refinancing_id with the refinancing id created. Example: I checked authorizations ids 2 and 3, so I create historic with two lines with authorization_origin_id 2 and 3 and the refinancing id will be 1. My code:
My view this it:
<%= simple_form_for(#refinancing) do |f| %>
<div class="form-inputs">
<%= f.hidden_field :employee_id, value: #employee.first.id %>
<%= f.hidden_field :authorization, value: #authorization %>
<%= f.input :contract_number %>
</div>
<h3>Reserved value</h3>
<table class="table table-condensed table-bordered table-striped">
<thead>
<th>Authorization id</th>
<th>Contract number</th>
</thead>
<% #authorizations.each do |authorization| %>
<tbody>
<tr>
<td><%= authorization.id %></td>
<td><%= authorization.contract_number %></td>
</tr>
</tbody>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit, "To Reserve" %>
</div>
<% end %>
Controller is this:
def new
if params[:authorization].present?
#selected_ids = params[:authorization][:contract_number]
#authorizations = Authorization.where("contract_number in (?)", #selected_ids)
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
#authorizations.ids.each do |auth_id|
#historic_refinancing = HistoricRefinancing.create
#historic_refinancing = HistoricRefinancing.update_all(authorization_origin_id: auth_id)
end
end
#employee = Employee.search_cpf(params[:search_employee_by_cpf])
#refinancing = Refinancing.new
end
Actually my table historic is wrong, authorization_origin_id take, just the last (just one ever =/) value checked and refinancing_id is null
The answer is it:
#historic_refinancing = HistoricRefinancing.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)
or simply this
#historic_refinancing = HistoricRefinancing.create(authorization_origin_id: auth_id)
Related
I have a site built using the railstutorial as a template. I have added a search controller to allow me to perform site searches and redirect the user to a search view. When I go to the path '/search' it is as expected (no results) but if I actually use the search input box I get logged out and have to log back in. What would be causing this?
My form:
<form action="/search" method="POST" class="navbar-search pull-right">
<input name="query" type="text" class="search-query" placeholder="Search">
</form>
My search controller:
class SearchController < ApplicationController
def index
unless params[:query].nil?
#results = ThinkingSphinx.search params[:query]
else
#results = []
end
end
end
My view:
<% unless #results.empty? %>
<table class="table">
<% #results.each do |result| %>
<tr>
<% if result.class.name == "Event" %>
<td><%= link_to result.name, organisation_event_path(result.organisation, result.slug) %></td>
<td><%= result.summary %></td>
<% end %>
</tr>
<% end %>
</table>
<% else %>
<p>No results found.</p>
<% end %>
My route:
match '/search', to: 'search#index'
It's related to CSRF issues. You can read the details here, or this SO question/answer.
This is my partial:
<%= form_for(item, :url => update_cart_path) do |item_form| %>
<tr>
<% #variant = item.variant %>
<%= hidden_field(:variant, :id) %>
<td width="300"><%=item.variant.product.name%> <%= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty? %></td>
<td class="price"><%= number_to_currency item.price %></td>
<td class="qty"><%=item.quantity%></td>
<td class="total"><span><%= number_to_currency (item.price * item.quantity)%></span></td>
<td class="edit"><%= link_to(image_tag('/images/admin/icons/edit.png'), '#', :class => 'edit') %>
</td>
</tr>
<% end %>
And I'm calling it as follows:
<%= render :partial =>'shared/cart/line_item' , :collection => #order.line_items, :as => :item %>
I have to create a local variable #variant for the hidden_field to work, and because it doesn't make any sense creating it in the controller, I have created the instance local variable in the view.
Is there a better design for this?
If you are only doing that for the hidden_field helper to work, you can use hidden_field_tag instead:
<%= hidden_field_tag('variant', item.variant.id) %>
I want to know how to get the selected value of <select>. I only know how to populate this.
Here's is my code in index.html.erb. I used this to populate the <select> dropdown menu.
<h1>Trap</h1>
<%= form_for #search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, #employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link #search, :empcode %> |
<%= sort_link #search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
And in my controller traps_controller.rb:
def index
#search = Trap.search(params[:search])
#traps = #search.all
#employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
Pls tell me how to get the selected value if the user selects a value. I don't have any idea.
Try (in the controller) :
value = params[:empcode_contains]
or you could fetch the whole object this way:
search = params[:search]
first off ive tried google and cant seem to find an answer i can understand what i would like to do is list my questions from an active record witch i have that much working then make the question its self a link so her is my page so far!
<h1>Listing faqs</h1>
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<% end %>
</table>
<br />
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<tr><td valign="top"><div id="right">Answer : </div></td><td><%= faq.answer %></td></tr>
<% end %>
</table>
<br />
<%= link_to 'New Faq', new_faq_path %>
im amusingenter code here i can stick something in before faq.question like a link_to or something or something but im not shure what
First of all, in Rails 3 forms should start with <%=, not <%.
<%= #faqs.each do |faq| %>
<tr>
<td width="90px">
<div id="right">Question : </div>
</td>
<td>
<%= link_to(faq.question, faq_path(faq.id) %>
</td>
</tr>
<% end %>
I have an action called 'list' and is defined like
def list
#subjects = Subject.order("subjects.position ASC")
end
And my list view looks like
<div>
<h2>Subjects</h2>
<%= link_to("Add new subject",{:action=>'new'},:class=>'action new')%>
<table class="listing" summary="Subject list">
<tr class="header">
<th> </th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<%= #subjects.each do |subject| %>
<tr>
<td><%=subject.position%></td>
<td><%=subject.name%></td>
<td class="center"><%=subject.visible ? 'Yes':'No' %></td>
<td class="center"><%=subject.pages.size%></td>
<td class="actions">
<%=link_to "Show",{:action=>'show',:id=>subject.id},:class=>'action show'%>
<%=link_to "Edit",{:action=>'edit',:id=>subject.id},:class=>'action edit'%>
<%=link_to "Delete",{:action=>'delete',:id=>subject.id},:class=>'action delete'%>
</td>
</tr>
<%end%>
</table>
</div>
So the problem is that besides getting an organized list of all the subjects nearby my link "Add subject" the page is printing all the information that #subjetcs brings from the controller.
Which of course is not a very good idea...I can't figure out why is that happening
Your problem is the line <%= #subjects.each do |subject| %>. Because you've got <%=, the return value of that line - which is #subjects - should be written into the document.
Use <% #subjects.each do |subject| %> instead to suppress the output.
<%= #subjects.each do |subject| %>
should be:
<% #subjects.each do |subject| %>
The equals sign tells ERB that the code should output something. You want to omit that in the iterator.
Remove the equals sign from the <%= #subjects.each do |subject| %> line.
ERB outputs everything within <%= ... %> tags to the page. So in this case, the return value of #subjects.each is written to the page. Thus, write your loop like this instead:
<% #subjects.each do |subject| %>
...
<% end %>
This applies to all statements that do stuff rather than output stuff to the page. For example, you would write an if statement as:
<% if some_condition %>
...
<% else %>
...
<% end %>
rather than as:
<%= if some_condition %>
...
<% else %>
...
<% end %>