I'm developing a project and I have a problem about this subject.
I'm constructing ruby on rails project which will provide listing students with name.
First of all , I will list my students information in a table and one part is missing.
When the user clicks on Student Name , another table will appear and show the other information of students(like course or GPA).
The table shows the students name ( this part is succeed.)
But I don't know how to post my variable when user clicks on the students name.
Moreover , I know fundementals of AJAX.But I don't know how to pass my variable while user clicks on students name in the table.
I don't use form , and I try to fix this problem with link_to_remote method but I can't post my variable.Because I can construct my table in for-loop according to my listing variable which contains whole database.I filter for getting only names and surnames in controller part of ruby on rails and i have listing variable for this.when in for-loop i use <%=student.name%> tag for displaying the part of my variable.and I must post this variable to page for filtering GPA or Course name according to names.I need something like link_to_remote method for posting my variable student.name
I'm waiting your help.
Code is below :
<table border="1">
<tr>
<th>
Name
</th>
</tr>
<% #listing.each do |student| %>
<tr>
<td>
<%=student.name%> <%= student.surname %>
</td>
</tr>
<% end %>
problem fixed ,:with => "'id=blabla'" in link_to_remote method
Related
In Rails I want to put a link in the view to the last object with some condition, I know how to found the object, like this code
Equipment.where(name: "Bulldozer").last
But I donĀ“t know the right syntax to put in the href to go to the show of the last Equipment with name Bulldozer
<td> text </td>
Thanks in advance!!!!
you're almost there, you need to assign the object to an instance variable, it'll be available within the view. So, in the controller action:
#equipment = Equipment.where(name: "Bulldozer").last
then, in your view:
<td>
<%= link_to "text", equipment_path(#equipment) %>
</td>
link_to helper will generate the <a> tag for you
So i have a rails var. It grabs informations from the table and puts it into the page (table)
Heres the controller:
giga = #event.gigaurl
#gigaresults = Gigatable.find_by_eventurl(gigantic)
Now if the #gigaresults is empty. The whole page crashes.
The table it appends to is this.
<tr>
<td>
<%= #gigaresults.title%>
</td>
</tr>
If any of these are empty it crashes the page (on heroku) Any ways to avoid this?
Sam
Use this:
<%= #gigaresults.try(:title) %>
will not crash even if #gigaresults is nil.
I have a dropdown in ruby on rails i.e below:
<table>
<tr>
<td align="center">
<%= f.select(:Interest,options_from_collection_for_select(#students, "id", "student_mentor_subjects"), {},:id => "DDL_Students", :style => "width:160px;") %>
</td>
</tr>
</table>
And students is a table that are populating f.select and below is the schema of students table:
id | student_mentor_subjects
1 | ijk
2 | mno
3 | pqr
And when I select a value from above f.select and click on search button then page is refresh and the selected value is lost, How could I retain the selected value of f.select after page refresh.
Kindly help me, Thanks.
From the api:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
The fourth argument to options_from_collection_for_select is the default value. In your case you need to set it from params, then fall back to a specific default if there's nothing in params for it (which would be the case when you load the page for the first time).
I don't know which value to read from params as you've not posted the rest of the form, but if you look in your log you should see which value you want to read. So, it might be something like
<%= f.select(:Interest,options_from_collection_for_select(#students, "id", "student_mentor_subjects", params[:search][:interest] || #students.first.id), {},:id => "DDL_Students", :style => "width:160px;") %>
I have a search page that display a search result. The search result is a list of persons that matched the specific search. I'm iterating through this list displaying them in a table. As headers for this table I want the DisplayName from the model. If I don't inherit IEnumerable I wouldn't be able to iterate through the list. I'm new at this MVC thing =)
I iterate through the result like this:
<% foreach (var item in Person) { %>
<%: item.surname %>
<% } %>
But how do I print the "DisplayName" of an attribute without iterating through the whole list? I would just like to do:
<%: Html.LabelFor(m => m.surname) %>
If it's any help I inherit an IEnumerable at the top of the page:
<%# Page Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>" %>
Edit
I want to display "Your surname" but I don't know how to access it from the view.
[DisplayName("Your surname")]
public object surname { get; set; }
Here's a very similar question that hasn't been answered either: Can I use LabelFor() when the page inherits from IEnumerable<T>?
If you only need to display specifics of one person; you should consider sending only one person to the view instead of a complete list of persons. In that case
Model.Surname
would work just like that. So instead of:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>"
do
Inherits="System.Web.Mvc.ViewPage<RegistryTest.Models.Person>"
In this case a single person is loaded into your Model and Model.Property works fine. If you want an IENumerable<>, think about what that means. You are sending a list of persons, so the only thing in your "Model" is a IENumerable<> of persons. There is no way that the view can know what you want if you call Model.Property, since in the Model there are multiple Objects and the view doesn't know which Object you want to get the Property from.
Bottom line, if you want to call Model.Property (Model.surname) but also want to send an IENumerable you are having a design flaw. If you send a list you should want to do something with the complete list (iterate through and do something with the contents). If you just want to do something with one person in that list, re-design your view/controller and send that single person; then you can use Model.Property.
//EDIT BASED UPON COMMENTS
As I see it now you either want to do one of those two things (I do not know which):
Show the records of an item in your list in a table and put the DisplayName of the current object shown in the table in the header.
Show all items of the list in your table and put some sort of DisplayName in the header. This makes less sence but it could be that you mean to name your list.
Situation 1
This is working as the rest of your code? The following would work just fine.
<% foreach (var item in Model) { %>
<table>
<th>item.DisplayName</th>
<tr>
<td>item.Property1</td>
<td>item.Property2</td>
</tr>
</table>
<% } %>
Situation 2
If you want a DisplayName of the list (??) you should create a ViewModel containing the IENumerable of Persons and a public string ListName. Now you can do something like:
<table>
<th>Model.ListName</th>
<% foreach (var item in Model) { %>
<tr>
<td>item.Property1</td>
<td>item.Property2</td>
</tr>
<% } %>
</table>
this would create a table with the name of your List (given in the ViewModel) as header and as items in the table you have your persons.
Design problem?
However, I would love to see you write some more information in your question above. Give us some more information on what you want to do. Do you want to show records of each Person in the list one-by-one? In that case I would recommend you create a Partial View where you put your table. Then you would get something like:
<% foreach (var item in Model) { %>
<% Html.RenderPartial("TablePerson",item); %>
<% } %>
tableperson.ascx:
...
Inherits="System.Web.Mvc.ViewPage<IEnumerable<RegistryTest.Models.Person>>"
...
<table>
<th>Model.DisplayName</th>
<tr>
<td>Model.Property1</td>
<td>Model.Property2</td>
</tr>
</table>
So, we need more information I'm afraid :)
If it's a collection with every entry having the same surname then try model[0].surname or model.ToList().First().surname
You don't need ToList() if its a List<T> already. Then it would be just model.First()
You are specifying that the page model is IEnumerable, and you say you would like to print a property of an element. Since you have a list of elements, you need to specify which of the elements you would like to retrieve the property from.
I you want a specific index in the list you will need to convert the IEnumerable collection to IList (ToList()), depending on the criteria, you may also be able to find the required element using something like a LINQ Single() operation.
Otherwise you could select the property from all the element in the list using Model.Select(m => m.PropertyName) which will give you a list of just this property, and then concatenate this list to a single string.
I've been working on an ASP.net MVC project that uses checkbox HTML Helpers in one of the views. The problem I'm trying to resolve is maintaining the checkbox state on a page refresh or when a user hits to he back button on their browser. I'm trying to use the HTML helper as a start, but I lack a complete understanding of how it works. I'm stuck on the third parameter that asks for HTML attributes. A snippet from a form that I have is as follows:
<tr>
<td><label for="Name">Name</label></td>
<td><%= Html.Encode(entity.CONTACT_NAME)%></td>
<td><input type="checkbox" name="Name" value="<%= Html.Encode(entity.CONTACT_NAME)%>"/> </td>
<td><%= Html.CheckBox("Name", false, new {#name = Html.Encode(entity.CONTACT_NAME)}) %></td>
</tr>
To avoid confusion, I have an object called entity that I declare before my form, that has several string values, one of which is CONTACT_NAME. I have a separate, standard, html checkbox right above the HTML Helper for testing purposes. When my form POSTs, I get the value if I select the standard checkbox, if I select the helper I get only true.
What I want to know is, how I can get
the value like I do when I select the
standard checkbox?
And how can I
maintain the checkbox state on page
refresh??
Any help is appreciated, thanks.
UPDATE:
#NickLarsen - Looked at the HTML code that was generated, the value is "false"
Made changes as per anthonyv's and JonoW's suggestions, my View is now as follows:
<tr>
<td><label for="Name">Name</label></td>
<td><%= Html.Encode(entity.CONTACT_NAME)%></td>
<td><%= Html.CheckBox("Name", false, new {#value = Html.Encode(entity.CONTACT_NAME)}) %></td>
</tr>
But the generated HTMl still shows the value as a boolean "false" instead of the actual CONTACT_NAME.
As per NickLarsen's suggestion, here is the code in my controller that checks the values.
public ActionResult ProcessRequest(Request request, FormCollection form)
{
var aChangeRequest = new ChangeRequest();
TryUpdateModel(aChangeRequest, new string[] { "Name" },form.ToValueProvider());
//Way more follows
}
should "#name" be "#value"? It looks like you are trying to set the name twice...
I'm pretty sure you want to have the following:
<%= Html.CheckBox("Name", false, new {#value = Html.Encode(entity.CONTACT_NAME)}) %>
If you have 2 inputs with the same name, they will be posted as a list of values (which MVC is going to try convert to a boolean value). So it's probably going to distort your test by having 2 elements with the same name, would suggest changing one of the names.