How to create edit forms in Turbogear - turbogears2

How to create edit forms. For url edit?id=1121 I want to display pre-filled data
EditForm(twf.Form):
class child(twf.TableLayout):
name= twf.TextField(name="name",value=DBSession.query(student.name).filter(student.id == <id passed in url>).distinct().all())
#expose ('edit')
def edit(self, id)
return dict(page='edit', , form=EditForm(action='/save')
Template:
<div>${form.display()}</div>

There are a few ways to achieve this, but I'd say that the cleanest one is passing the values to the form from the controller action as showcased by http://turbogears.readthedocs.io/en/latest/turbogears/widgets_forms.html#displaying-forms
In the case of your specific example it should result in having a form that only declares the fields that need editing and a reference to the edited object id:
class EditForm(twf.Form):
class child(twf.TableLayout):
student_id = twf.HiddenField()
name = twf.TextField(name="name")
Then within the controller you can fetch the edited object and provide the values to the form:
#expose('edit')
def edit(self, id):
edited_student = DBSession.query(student).filter(student.id==id).first()
return dict(page='edit',
form=EditForm(action='/save',
value=dict(student_id=id,
name=edited_student.name))
Keep in mind that this is just a proof of concept, I haven't tested the code and it lacks proper validation/error handling. But it should pretty much give the idea that you just pass the student name to the form through the value parameter when displaying it.

Related

Grails - How to send a personInstance ID from gsp to domain class?

I'm a complete newbie in grails and I need you guys' help.
I have my sql query in the domain class. I put [1] to see the result but ultimately I'd like to send an argument in that place to display the result according to the person's id number.
def dataSource
def someMethod() {
def sql = new Sql(dataSource)
def resultRows = sql.rows('select * from result where id = ?', [1])
}
And this is what I have in my gsp.
<g:each in="${personInstance.someMethod()}" status="i" var="results">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>${results.column_1}</td>
<td>${results.column_2}</td>
<td>${results.column_3}</td>
</tr>
</g:each>
How do I send a parameter from view to domain class?
Please help.
Thank you in advance.
If you want to see how Grails 'would like' your controller and view code to look, try letting Grails generate the code for you. Even if you don't keep that code in your final project, it is still useful as an instructional tool.
arc$ grails create-app Tester1
arc$ cd Tester1
arc$ grails
grails> create-domain-class Person
-- add some attributes to your Person domain class, save the file
grails> generate-all tester1.Person
Now go look at the PersonController.groovy, and the various views. Basically, it's marshal your data in the controller, pass it to the views, views operate on what they're given.
Very basic example of passing arbitrary data to the gsp:
// show method for an Adventure
def show(Adventure adventure) {
// a String to pass to the gsp
def attribute = 'Bilbo'
// an Array to pass to the gsp
def attributeList = ['Dwalin','Balin','etc']
// create a map of values that are 'automagically' passed
// to the show.gsp
[adventure: adventure, hobbit: attribute, dwarves: attributeList]
}
The adventure, hobbit, and dwarves variables are all available in the gsp. The template code likes to use verbose naming, like adventureInstance, but as long as your gsp code uses the keynames in the map you define, you're good to go.

sitecore mvc value of droplink

I am looking for the simplest way to get the referenced item value for a droplink field.
#Html.Sitecore().Field("Alignment")
I want to get the value of the choice, what's the best approach?
If you need to have ability to edit fields of alignment item which is chosen in 'Alignment' droplink field of context item or just show values of alignment item's fields for visitors:
#{
Sitecore.Data.Fields.ReferenceField alignmentField = Sitecore.Context.Item.Fields["Alignment"];
Sitecore.Data.Items.Item alignmentItem = alignmentField.TargetItem;
}
<div>
#Html.Sitecore().Field("Text of Alignment", alignmentItem)
</div>
This example assumes that Alignment template contains 'Text of Alignment' field.
The Droplink field stores the referenced item's ID. To retrieve this ID (providing the field is present in your current item/model):
((LinkField)Model.Item.Fields["Alignment"]).Value
To output the referenced item's name, you could do something like this:
#(Model.Item.Database.GetItem(((LinkField)Model.Item.Fields["Alignment"]).Value).Name)
But that's really ugly. The preferred approach would be to create an extension method encapsulating some of the above so you're not having to re-type that out :D
The article Extending the SitecoreHelper Class by John West shows how to extend the SitecoreHelper class to add custom field renderers, so you could end up creating a neat re-usable snippet like:
#(Html.Sitecore().ReferenceField("Alignment","Name"))
If this is in a partial view i.e. .cshtml file you can also use something like below:
Sitecore.Data.Fields.TextField alignment= Model.Item.Fields["Alignment"];
This will give you the id of the set item in the drop link , then from that id can retrieve it from the database like:
#if (!string.IsNullOrWhiteSpace(alignment.Value))
{
var setAlignment = Sitecore.Context.Database.GetItem(alignment.Value);
if (setAlignment != null && !string.IsNullOrWhiteSpace(setAlignment.Name))
{
setAlignment.Name
}
}
Personally i prefer this way as i can check if the droplink is set before trying to use the value.

Overload of actions in the controller

Is it possible to do an overload of the actions in the controller? I haven't found any info about it and when I tried, I got this error:
The current request for action 'Create' on controller type 'InterviewController' is >ambiguous between the following action methods:
System.Web.Mvc.ViewResult Create() on type >MvcApplication4.MvcApplication4.InterviewController
System.Web.Mvc.ViewResult Create(Int32) on type >MvcApplication4.MvcApplication4.InterviewController
I've tried to do this on another way and I also get a new error that I can't fix. In fact, I created a new action (called create_client instead of create)
I need 2 ways of creating an "opportunite".
I just call the action, and I receive an empty formular in which I just have to insert data.
From a client's page, I must create an "opportunite" with the client that's already completed when the form is displayed to the user. (there is a need of productivity, the user must perform actions as fast as possible).
In the table "opportunite", I've got a column called "FK_opp_client", which is equal to the column "idClient" from the client's table.
I don't get how I can do the second way.
I've created a new action in the controller.
'
' GET: /Opportunite/Create_client
Function Create_client(idclient) As ViewResult
'Dim FK_Client = (From e In db.client
'Where(e.idClient = idclient)
' Select e.nomCompteClient).ToString()
'ViewBag.FK_client = New SelectList(db.client, "idClient", "nomCompteClient", idclient)
Dim opportunite As opportunite = db.opportunite.Single(Function(o) o.idOpportunite = 5)
opportunite.FK_Client = idclient
ViewBag.FK_Client = New SelectList(db.client, "idClient", "nomCompteClient", opportunite.FK_Client)
Return View(opportunite)
End Function
I've tried a few things to get what I wanted, the last one was to copy what was done in the "Edit" action, but for an empty rank. (so I created an empty rank in my DB). I don't think it was a good idea (imagine someone wants to update the DB where idOpportunite = 5...)
Any better ideas?
If you want to keep those two methods under the same name, you will have to implement an ActionSelectionAttribute to decorate them, or use them with different verbs (for example POST and PUT). Please read more details on action method selection process here (old but still true).
Different approach might be making your parameter optional and make action to check if it has been passed or not (through nullable type).

SPListItem id from SPField object

I am working on create a custom field type and for implementation issue I need to retrieve the ID of the SPListItem in the SPField class which belong to these field type but I can't retrieve it.
For example:
public class myField:SPFieldText
{
// I need ListItemID in this class
}
Can anyone help me please?
SPFieldText is an SPField, which is the schema definition for a field. Its like saying, given an SQL create table statement, give me the id of row x. Can't be done.
I think the logic you are trying to perform should be done in an event receiver, so say when an item is saved, you take the ID and add it to the text field.
I didn't find a solution but I've tried another solution which is
I can get the ID of the item in the New and Edit forms so I saved the ID and the field value as a one value separated by '/' and in the SPFieldText class I've been able to retrieve the ID value from the value of the field

Rails3: Cascading Select Writer's Block

I have a big, flat table:
id
product_id
attribute1
attribute2
attribute3
attribute4
Here is how I want users to get to products:
See a list of unique values for attribute1.
Clicking one of those gets you a list of unique values for attribute2.
Clicking one of those gets you a list of unique values for attribute3.
Clicking one of those gets you a list of unique values for attribute4.
Clicking one of those shows you the relevant products.
I have been coding Rails for about 4 years now. I just can't unthink my current approach to this problem.
I have major writer's block. Seems like such an easy problem. But I either code it with 4 different "step" methods in my controller, or I try to write one "search" method that attempts to divine the last level you selected, and all the previous values that you selected.
Both are major YUCK and I keep deleting my work.
What is the most elegant way to do this?
Here is a solution that may be an option. Just off the top of my head and not tested (so there is probably a bit more elegant solution). You could use chained scopes in your model:
class Product < ActiveRecord::Base
scope :with_capacity, lambda { |*args| args.first.nil? ? nil : where(:capacity=>args.first) }
scope :with_weight, lambda { |*args| args.first.nil? ? nil : where(:weight=>args.first) }
scope :with_color, lambda { |*args| args.first.nil? ? nil : where(:color=>args.first) }
scope :with_manufacturer, lambda { |*args| args.first.nil? ? nil : where(:manufacturer=>args.first) }
self.available_attributes(products,attribute)
products.collect{|product| product.send(attribute)}.uniq
end
end
The code above will give you a scope for each attribute. If you pass a parameter to the scope, then it will give you the products with that attribute value. If the argument is nil, then the scope will return the full set (I think ;-). You could keep track of the attributes they are drilling down in in the session with 2 variables (page_attribute and page_attribute_value) in your controller. Then you call the entire chain to get your list of products (if you want to use them on the page). Next you can get the attribute values by passing in the set of products and the attribute name to Product.available_attributes. Note that this method (Product.available_attributes) is a total hack and would be inefficient for a large set of data, so you may want to make this another scope and use :select=>"DISTINCT(your_attribute)" or something more database efficient instead of iterating thru the full set of products as I did in the hack method.
class ProductsController < ApplicationController
def show
session[params[:page_attribute].to_sym] = params[:page_attribute_value]
#products = Product.all.with_capacity(session[:capacity]).with_weight(session[:weight]).with_color(session[:color]).with_manufacturer(session[:manufacturer])
#attr_values = Product.available_attributes(#products,params[:page_attribute])
end
end
Again, I want to warn you that I did not test this code, so its totally possible that some of the syntax is incorrect, but hopefully this will give you a starting point. Holla if you have any questions about my (psuedo) code.