I am trying to write a tickets booking system.
I have a 2d array stored the seat and the status
#movie1 = [["A1", 0], ["A2", 0], ["A3", 1],["A4", 1], ["A5", 1], ["A6", 1], ["A7", 1], ["A8", 1], ["A9", 0], ["A10", 1]]
And this is the view
<form action= "seat/test4" method="get">
<table>
<% for i in 0..9 do %>
<% if i == 0 then %>
<tr>
<% end %>
<% if #movie1[i][1] == 1 then %>
<td bgcolor="red">
<%= #movie1[i][0] %>
<input type = button value = "X"
</td>
<% else %>
<td bgcolor="green">
<%= #movie1[i][0] %>
<input type="checkbox" id= <%= #movie1[i][0] %> name = <%= #movie1[i][0] %> >
</td>
<% end %>
<% if i == 9 then %>
</tr>
<% end %>
<% end %>
</table>
<input type="submit" value="Purchase">
</form>
How can I get the checkbox value and change the #movie1[i][1] = 1 ?
If you are trying to change #movie1. #movie1 is a ruby variable it renders when page loads and it cannot be changed after that. if you really want to change the variable without page reload you can do it via Ajax call Rails itself has feature to accomplish that.
To know more refer this link:
Working with javascript in Rails
Also Refer about rails partial
Rails Partial
This is used to create subtemplates, is usefull for dynamic cases like this.
As a Suggestion you can also implement the view as follows
<form action= "seat/test4" method="get">
<table>
<tr>
<% #movie1.each do |m| %>
<% if m[1] == 1 %>
<td bgcolor="red">
<%= m[0] %>
<input type = button value = "X" />
</td>
<% else %>
<td bgcolor="green">
<%= m[0] %>
<input type="checkbox" id= <%= m[0] %> value="<%= m[0] %>" name = <%= m[0] %> />
</td>
<% end %>
<% end %>
</tr>
</table>
<input type="submit" value="Purchase">
</form>
Also add value attribute to checkbox tag
Related
I'm new to Rails and I'm trying to build a nice application and I'm struggling with arrays, I have 4 arrays that I want to iterate and they are not the same size
I want to generate sections in HTML using the first array what I did it
#sections = ['Section One','Section Two','Section Three','Section Four']
#itemsOne = ['item 1','item 2','item 3','item 4','item 5','item 6']
#itemsTwo = ['item 1','item 2','item 3','item 4','item 5','item 6']
I was using
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t1, t2, t3| %>
<%= t1 %>
<table>
<tbody>
<tr>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
</tr>
</tbody>
</table>
<% end %>
I have a table that have a Section Title and cells that have two values
but what I get is the value of |t2| in each cell of |t1| section
using #Phil answer down below, but he deleted it.
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t| %>
<%= t[0] %>
<table>
<tbody>
<tr>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
</tr>
</tbody>
</table>
<% end %>
p.s. the itemsOne and itemsTwo arrays have more than 20 values.
What I created is separated my big arrays into smaller ones and then Iterated thru each this way without a Table because table was making design issues so i went into div's using bootstrap 3 column, there might be a better way but this is what i got as a beginner.
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[0] %></h4>
<!-- This will Display Section 0 in the Array -->
</div>
<div class="row">
<div class="col-md-12">
<% #count = 0 %>
<!-- Counter is Zero -->
<% #ItemsOne.collect do |t1| %>
<!-- This will loop array to increment the #count and repeat the HTML -->
<% #count += 1 %>
<!-- With each loop increment by 1-->
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #ItemsOne[#count - 1] %>
<!-- Counter should start from 0 adding -1 will make it start
from 0 instead of 1 and then will print the value of the Index Number -->
</label>
</div>
</div>
<% end %>
</div>
</div>
</div>
Here is another way to do this
<div class="row">
<% #Sections.each_with_index do |x1, n| %>
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[n] %></h4>
</div>
<div class="row">
<div class="col-md-12">
<% if n == 0 %>
<% #itemsOne.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsOne[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 1 %>
<% #itemsTwo.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsTwo[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 2 %>
<% #itemsThree.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsThree[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 3 %>
<% #itemsFour.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsFour[n] %>
</label>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
What's the most efficient way to display a date in a view in dd/mm/yyyy format?
I modified config/initializers/date_time.rb as such:
# Date
Date::DATE_FORMATS[:default] = "%d/%m/%Y"
However, I have a dropdown list that contains dates, and I use that to pass a date parameter date_id for searching purpose. If I use the above line, it doesn't return any results when it should.
My code:
balances_controller.rb
def statement
#dates = Balance.select("distinct(date)").order('date desc')
if (params[:date_id].blank?)
#latestDate = Balance.order('date desc').first.date
else
#latestDate = params[:date_id]
end
#summaryBalances = Balance.joins(:account).order('accounts.credit').where('date = :abc', {abc: #latestDate})
end
statement.html.erb
<h1>Statement at <%= #latestDate %></h1>
<%= form_tag("/statement", method: "get") do %>
<%= select_tag "date_id", options_for_select( #dates.collect {|d| [ d.date, d.date ] }) %>
<%= submit_tag("Search") %>
<% end %>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Account</th>
<th>Balance</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #summaryBalances.each do |balance| %>
<tr>
<td><%= balance.account.name %></td>
<td class='account-balance'><%= number_to_currency(balance.balance, unit: "£") %></td>
<td><%= link_to 'Edit', edit_balance_path(balance) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
UPDATE:
html output:
<form action="/statement" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓" />
<select name="date_id" id="date_id"><option value="2015-11-30">30/11/2015</option>
<option value="2015-10-31">31/10/2015</option>
<option value="2015-09-30">30/09/2015</option></select>
<input type="submit" name="commit" value="Search" />
</form>
I'm not sure if this is the most efficient way of resolving this but I've managed to do what I need it to do by creating two separate helper methods. One to display the date in dd/mm/yyyy format and one to return the date in yyyy-mm-dd format for the parameter.
def date_format(date)
date.to_date.strftime("%d/%m/%Y")
end
def date_format2(date)
date.to_date.strftime("%Y-%m-%d")
end
Updated statement.html.erb
....
<%= form_tag("/statement", method: "get") do %>
<%= select_tag "date_id", options_for_select( #dates.collect {|d| [ date_format(d.date), date_format2(d.date) ] }) %>
<%= submit_tag("Search") %>
<% end %>
....
I'm having a problem with my advanced search.
Essentially, its an advanced search for bands (name, style, bandpicture by checkboxes (checked: have a bandpicture))
Here is the code.
result.html.erb
<table >
<colgroup>
<col width="250" />
</colgroup>
<tr>
<td>
<label for="name">Filter</label></br>
<%= form_tag band_path do %>
<label for="Style">Style</label></br>
<%= select_tag :style_id, options_from_collection_for_select(Style.all, :id, :name, params[:style_id]), include_blank: true, class: "form-control" %>
<br>
</br>
<%= check_box_tag 'options[]', "picture" %>
<%= button_to '#', class: 'btn btn-default' do %>
<%= t(:find) %>
<% end %>
<% end %>
</td>
<td>
<div>
<% #bands.each do |band|%>
<div class="top-bar">
<div class="center">
<div class="info">
<div class="name"><%=band.id %></div>
<div> <%= band.zip %>, <%= band.city %> </div>
</div>
<div class="desc">
<table>
<tr>
<td>Styles</td>
<td>
<% band.styles.each do |s| %>
<%= s.style.name %>
<% end %>
</td>
</tr>
</table>
</div>
<% end %>
</td>
</tr>
</table>
models band.rb
def self.search1(style_id)
Profile.joins("...").where("style_id = ?", style_id)
end
controller.
def searchresult
if params[:style_id].present?
#bands = Band.search1 params[:style_id]
elsif params[:options]
Band.where(:options => #filtered_options)
else
#bands = Band.all
end
end
The searchresult don't show the result of the checkboxes and I have this errors:
undefined method `search1' for #<Class:0x00000008eb5548>
I'm using Steve Sanderson's BeginCollectionItem helper with ASP.NET MVC 2 to model bind a collection if items.
That works fine, as long as the Model of the collection items does not contain another collection.
I have a model like this:
-Product
--Variants
---IncludedAttributes
Whenever I render and model bind the Variants collection, it works jusst fine. But with the IncludedAttributes collection, I cannot use the BeginCollectionItem helper because the id and names value won't honor the id and names value that was produced for it's parent Variant:
<div class="variant">
<input type="hidden" value="bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126" autocomplete="off" name="Variants.index">
<input type="hidden" value="0" name="Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].SlotAmount" id="Variants_bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126__SlotAmount">
<table class="included-attributes">
<input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id">
<tr>
<td>
<input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id">
</td>
</tr>
</table>
</div>
If you look at the name of the first hidden field inside the table, it is Variants.IncludedAttributes - where it should have been Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].IncludedAttributes[...]...
That is because when I call BeginCollectionItem the second time (On the IncludedAttributes collection) there's given no information about the item index value of it's parent Variant.
My code for rendering a Variant looks like this:
<div class="product-variant round-content-box grid_6" data-id="<%: Model.AttributeType.Id %>">
<h2><%: Model.AttributeType.AttributeTypeName %></h2>
<div class="box-content">
<% using (Html.BeginCollectionItem("Variants")) { %>
<div class="slot-amount">
<label class="inline" for="slotAmountSelectList"><%: Text.amountOfThisVariant %>:</label>
<select id="slotAmountSelectList"><option value="1">1</option><option value="2">2</option></select>
</div>
<div class="add-values">
<label class="inline" for="txtProductAttributeSearch"><%: Text.addVariantItems %>:</label>
<input type="text" id="txtProductAttributeSearch" class="product-attribute-search" /><span><%: Text.or %> <a class="select-from-list-link" href="#select-from-list" data-id="<%: Model.AttributeType.Id %>"><%: Text.selectFromList.ToLowerInvariant() %></a></span>
<div class="clear"></div>
</div>
<%: Html.HiddenFor(m=>m.SlotAmount) %>
<div class="included-attributes">
<table>
<thead>
<tr>
<th><%: Text.name %></th>
<th style="width: 80px;"><%: Text.price %></th>
<th><%: Text.shipping %></th>
<th style="width: 90px;"><%: Text.image %></th>
</tr>
</thead>
<tbody>
<% for (int i = 0; i < Model.IncludedAttributes.Count; i++) { %>
<tr><%: Html.EditorFor(m => m.IncludedAttributes[i]) %></tr>
<% } %>
</tbody>
</table>
</div>
<% } %>
</div>
</div>
And the code for rendering an IncludedAttribute:
<% using (Html.BeginCollectionItem("Variants.IncludedAttributes")) { %>
<td>
<%: Model.AttributeName %>
<%: Html.HiddenFor(m => m.Id, new { #class = "attribute-id" })%>
<%: Html.HiddenFor(m => m.ProductAttributeTypeId) %>
</td>
<td><%: Model.Price.ToCurrencyString() %></td>
<td><%: Html.DropDownListFor(m => m.RequiredShippingTypeId, AppData.GetShippingTypesSelectListItems(Model.RequiredShippingTypeId)) %></td>
<td><%: Model.ImageId %></td>
<% } %>
As you are using MVC 2 and EditorFor, you shouldn't need to use Steve's solution, which I believe is just a work around for MVC 1. You should just be able to do something like:
<% for (int i = 0; i < Model.Variants.Count; i++) { %>
<%= Html.DisplayFor(m => m.Variants[i].AttributeType.AttributeTypeName) %>
<% for (int j = 0; j < Model.Variants[i].IncludedAttributes.Count; j++) { %>
<%= Html.EditorFor(m => m.Variants[i].IncludedAttributes[j]) %>
<% } %>
<% } %>
Please note that the use of the indexes ...[i]...[j]... is important and is how MVC will know how to render the Id's and names correctly.
This is the ascx page:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<OpenForum.Core.ViewModels.IndexViewModel>" %>
<%# Import Namespace="OpenForum.Core.Views.ForumViewHelper" %>
<div class="openforum_container">
<%= (Model.IncludeDefaultStyles) ? ForumViewHelper.GetDefaultStyles() : ""%>
<%= (Model.IncludeValidationSummary) ? Html.ValidationSummary() : ""%>
<div class="openforum_actions">
<%= Html.ActionLink("Write A New Post", "Post") %>
</div>
<div class="openforum_search">
<% Html.BeginForm(); %>
Search: <%= Html.TextBox("searchQuery") %> <input type="submit" value="go" />
<% Html.EndForm(); %>
</div>
<div class="openforum_message"><%= Model.Message %></div>
<table class="openforum_maincontent">
<% foreach(var item in Model.Posts ?? new Post[0]) { %>
<tr>
<td class="openforum_title"><%= Html.ActionLink(item.Title, "view", new { id = item.Id, title = ForumViewHelper.ToUrlFriendlyTitle(item.Title) })%></td>
<td class="openforum_user">
<div>created by <%= Html.Encode(item.CreatedBy.Username) %></div>
<div><%= item.CreatedDate.ToString("MM/dd/yyyy hh:mm tt") %></div>
</td>
<td class="openforum_modified">
<div>last post by <%= Html.Encode(item.LastPostBy.Username) %></div>
<div><%= item.LastPostDate.ToString("MM/dd/yyyy hh:mm tt") %></div>
</td>
<td class="openforum_replies">Replies: <%= item.Replies.Length %></td>
<td class="openforum_views">Views: <%= item.ViewCount %></td>
</tr>
<% } %>
</table>
<div class="openforum_index_paging">
<% if ((Model.Posts ?? new Post[0]).Count() > 0) { %>
<span>Page <%= Model.CurrentPage + 1 %> of <%= Model.TotalPages %></span>
<% } %>
<% if (Model.CurrentPage > 0) { %>
<% Html.BeginForm(); %>
<input type="submit" value="<<<" />
<input type="hidden" name="searchQuery" value="<%= Model.SearchQuery %>" />
<input type="hidden" name="page" value="<%= Model.CurrentPage - 1 %>" />
<% Html.EndForm(); %>
<% } %>
<% if (Model.CurrentPage < Model.TotalPages - 1) { %>
<% Html.BeginForm(); %>
<input type="submit" value=">>>" />
<input type="hidden" name="searchQuery" value="<%= Model.SearchQuery %>" />
<input type="hidden" name="page" value="<%= Model.CurrentPage + 1 %>" />
<% Html.EndForm(); %>
<% } %>
</div>
</div>
I keep getting an error: The name 'ForumViewHelper' does not exist in the current context. I am using the OpenForum dll. I called the Html.RenderPartial("Index.ascx").
I think the issue is that "ForumViewHelper" is a class inside the "OpenForum.Core.Views" namespace. Try using this instead...