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
Related
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'm trying to do something that seems very simple: create a form that submits to same URL it was requested from, with an id.
If I didn't care about the id, I could do:
<% using(Html.Form()) { %>
<!-- stuff -->
<% } %>
But since I want the id, I have to use a different overload.
I would like something along the lines of:
<% using(Html.Form(some, args, new {id="myAwesomeForm"})) { %>
<!-- stuff -->
<% } %>
I can't just hardcode the action and controller because the form is used in a couple of different places. Sometimes the URL will have parameters (/items/edit/1, and other times it will not /items/create)
There must be some incredibly simple way of doing this that is going to make me feel like an idiot when I see it. So, what is it?
Clarification: I mean an id on the HTML element, as in <form action="/my/action[/possible arguments]" id="myAwesomeForm"></form>
Use null for the action and controller; they will be filled in from the current action and controller.
<% using (Html.BeginForm( null, null, null, FormMethod.Post, new { id = 3 } )) { %>
<% } %>
Just use the first overload (routeValues As Object)
It will assume the current, Area Name, Controller Name and Action Name parameters. Post is the default form method.
<%
Using Html.BeginForm(New With {.id = 3})
End Using
%>
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.
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
How should I initiate a delete action from my view?
Creating a new form-tag for each entity just doesn't seem right :-)
<% foreach (var subscriber in group.Subscribers) { %>
<tr>
<td><%= subscriber.Email %></td>
<td><%= Html.ActionLink("[edit]", "edit", "subscriber", new {id=subscriber.SubscriberId}, null) %></td>
<td>
<form id="delete-subscriber-form" method="post" action="<%= Url.Action( "delete", "subscriber", new { #subscriberId = subscriber.SubscriberId }) %>">
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<% } %>
How would you do it?
I normally use checkboxes on the side of the items. Then I can have action links (buttons, whatever) that apply an action to the selected items (such as delete).
you can use CSS and Javascript , add 'forDel' css class for all the elments you want to delete , if you are going to use jquery you can do it like this:
$(".element").each(function(){
$(this).data("id","the id of the element in db")
});
$(".element").toggle(function(){
$(this).addClass("forDel");
},function(){
$(this).removeClass("forDel");
});
and then on pressing the delete button:
var idsForDel;
$(".forDel").each(function(){
idsForDel = $(this).data("id"); + ";";
})
you can pass the idsForDel to the Controller ... and split it in the server side.
It depends on the situation, if you are doing CRUD operations you would normally go with one <form> tag per operation (delete,edit,new). If, however, you are displaying a list and you want to be able to 'multiple delete' items with one click then you will have to approach it from a different angle as you need to encapsulate all the required information into one form.
EDIT
Having had another look at your post above I notice you are providing a 'Delete Button' against each element in the list. For atomic actions like this (i.e. the user expects something to happen straight after they have clicked a button) I would definitely use one form per item.
What I wrote above still applies...