rails rails3-jquery-autocomplete MissingAttributeError for display_value - autocomplete

I'm using the rails3-jquery-autocomplete gem to autocomplete the name of a user, search attribute is lastname but I want to display full name as the display value. Here's what I have in my controller
autocomplete :customer, :lastname, :display_value => :display_autocomplete
Customer Model
def display_autocomplete
self.firstname + ' ' + self.lastname
end
Error:
ActiveModel::MissingAttributeError (missing attribute: firstname):

You need to get the extra data, like so:
autocomplete :customer, :lastname, :display_value => :display_autocomplete, :extra_data => [:firstname, :lastname]

Related

ZendFramework - How to get Page ID Name

I have a table *sitepage_manageadmins* which contain: " user_id, page_id ".
There is another table *sitepage_pages* which contain " page_id, title ".
Im trying to render as dropdown ( select list ) in Zend_Form the user which is admin to relevant page_id .
Here is my code:
$this->view->owner_id = $viewer_id = $viewer->getIdentity(); // get $viewer
$adminpages = Engine_Api::_()->getDbtable('manageadmins', 'sitepage')->getManageAdminPages($viewer_id); // get viewer page_id's where is admin
Getting the page_id as dropdown list:
$ids = array ( 0 => '-- Select --');
foreach ($adminpages as $adminpage) {
$ids[] = $adminpage->page_id;
}
Rendering the dropdown
$this->addElement('select', 'page_id', array (
'label' => 'Page where I'm Admin',
'multioptions' => $ids,
));
For now, im only render the Page_ID numbers as dropdown. I want from Specific Page_ID to render the Title.
Any ideas are welcome?
Thanks
This is because you are putting only the ids to the $ids[] array The multioption array should be key-value pair in this case page_id as key & page_title as value.
foreach ($adminpages as $adminpage) {
$ids[$adminpage->page_id] = $adminpage->page_title;
}

How can I use a local (or per view) variable in Sinatra with Haml partials?

I have a Haml partial in Sinatra to handle all of my 'page open' items like meta tags.
I would love to have a variable for page_title in this partial and then set that variable per view.
Something like this in the partial:
%title #page_title
Then in the view, be allowed to do something like:
#page_title = "This is the page title, BOOM!"
I have read a lot of questions/posts, etc. but I don't know how to ask for the solution to what I am trying to do. I'm coming from Rails where our devs usually used content_for but they set all that up. I'm really trying to learn how this works. It seems like I have to define it and use :locals in some way but I haven't figured it out. Thank you in advance for any tips!
You pass variables into Sinatra haml partials like this:
page.haml
!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => "BOOM!"}
_header.haml
%head
%meta{:charset => 'utf-8'}
%title= locals[:title]
In the case of a page title I just do something like this in my layout btw:
layout.haml
%title= #title || 'hardcoded title default'
Then set the value of #title in routes (with a helper to keep it short).
But if your header is a partial then you can combine the two examples like:
layout.haml
!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => #title}
_header.haml
%head
%meta{:charset => 'utf-8'}
%title= locals[:title]
app.rb
helpers do
def title(str = nil)
# helper for formatting your title string
if str
str + ' | Site'
else
'Site'
end
end
end
get '/somepage/:thing' do
# declare it in a route
#title = title(params[:thing])
end

Ruby on Rails Formtastic Multiple Options in Member_Label

I have a relational model where users have managers that are also users. The below code works great and does exactly what it's suppose to, but it's only displaying the first name of the manager. I'm trying to get this to show both the first name and the last name of the manager.
<%= sf.input :managers, :as => :check_boxes, :member_label => (:firstname) ,:input_html => { :size => 20, :multiple => true}%>
The other field i'm trying to add is the :lastname. I cannot figure out how to get :member_label to take both fields.
I figured it out. By using the Proc.new, I was able to add in both first name and last name.
<%= sf.input :managers, :as => :check_boxes, :member_label => Proc.new { |t| h(t.firstname + " " + t.lastname) } ,:input_html => { :size => 20, :multiple => true}%>

Yii CJuiAutoComplete data from Ajax request

I have a task form for an application I am building which allows me to specify what contact it is assigned to. I had set up a dependent dropdown in Yii with the following code:
echo $form->dropDownList($model,'associationType',
array('none'=>'None','contact'=>'Contact','sale'=>'Sale','account'=>'Account',
'project'=>'Project','case'=>'Case'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('tasks/parseType'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#auto_complete', //selector to update
)
));
What I'm trying to do is use the CJuiAutoComplete widget with the dropDown specifying which array to grab. So the drop down is selected to contacts, it should get a list of contacts etc.
Here is what I have for the CJui widget
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'auto_select',
'source' => $names,
'options'=>array(
'minLength'=>'2',
'select'=>'js:function( event, ui ) {
$("#'.CHtml::activeId($model,'associationId').'").val(ui.item.id);
return false;
}',
),
));
The variable $names is just a placeholder for now, but in my controller method I pass it a JSON encoded array with id and name. Controller code:
public function actionParseType() {
//if(isset($_POST['TaskChild']['associationType'])){
//$type=$_POST['TaskChild']['associationType'];
$type='sale';
$sql = 'SELECT id, name FROM x2_'.$type.'s';
$cmd = Yii::app()->db->createCommand($sql);
$res = $cmd->queryAll();
echo CJSON::encode($res);
//}
}
Right now I'm forcing it to use "Sale" but I don't get anything when I call the method, and was wondering how I might go about fixing this. I'm still a little new to Yii so I've been mostly reading wiki/forum posts on how these kinds of thing are done. Any help is greatly appreciated, thanks!
Try something like this in your controller action:
$sql = 'SELECT people_id as id, CONCAT(first_name," ",last_name) as value, first_name as label FROM people WHERE first_name LIKE :qterm ORDER BY first_name ASC';
$command = Yii::app()->db->createCommand($sql);
$qterm = $_GET['term'].'%';
$command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
$result = $command->queryAll();
echo CJSON::encode($result); exit;
Then you can check it by using this in your widget 'options' array: 'select'=>'js:function(event, ui) { console.log(ui.item.id +":"+ui.item.value); }'

How to save the id of a Zend_Dojo_Form_Element_ComboBox not the text

Can anyone tell me how I can save the ID of a Dojo ComboBox element in Zend. Sorry if this is simple but I can't figure out the answer.
My combobox code is as follows
// Create a autocomplete input for counties
$county = new Zend_Dojo_Form_Element_ComboBox('county');
$county->setLabel('County');
$county->setOptions(array(
'autocomplete' => true,
'storeId' => 'countyStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array('url' => "/clients/client/autocomplete"),
'dijitParams' => array('searchAttr' => 'county')))
->setRequired(true)
->addValidator('NotEmpty', true)
->addFilter('HTMLEntities')
->addFilter('StringToLower')
->addFilter('StringTrim');
The code for my autocompleteAction is as follows
public function autocompleteAction()
{
// disable layout and view rendering
$this->_helper->layout->disableLayout();
$this->getHelper('viewRenderer')->setNoRender(true);
// get list of breed names from the breeds table
$qry= Doctrine_Query::create()
->from('PetManager_Model_Counties c');
$result=$qry->fetchArray();
//generate and return JSON string countiesID being the primary key of the table
$data = new Zend_Dojo_Data('countiesID',$result,'countiesID');
echo $data->toJson();
}
The form is an input form for saving new clients. The table that it manipulates has a field county that references a table counties and hence only accepts Int values.
I want to save the primary key of the county 'countiesID' from that selected in the Dojo ComboBox, currently only the text selected is passed i.e. 'Dublin' not '10', which of course throws an exception when I try to save the record.
I know how to do this with a standard combobox in that I would create a function that would query the counties table and use a foreach array to add the primary key and county name to the form via addMultiOption but I can figure out how to do this when using the Dojo autocomplete combobox.
I've actually sorted this with help from the Zend Community.
You actually have to use FilteringSelect instead of ComboBox to save the record ID as follows
// Create a autocomplete select input for counties
$county = new Zend_Dojo_Form_Element_FilteringSelect('county');
$county->setLabel('County');
$county->setOptions(array(
'autocomplete' => true,
'storeID' => 'countyStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array('url' => "/clients/client/autocomplete"),
'dijitParams' => array('searchAttr' => 'county')))
->setRequired(true)
->addValidator('NotEmpty', true)
->addFilter('HTMLEntities')
->addFilter('StringToLower')
->addFilter('StringTrim');
I hope this helps you ZendNewbie and anyone else new to the framework.