How can I disable this message? - ruby-on-rails

I'm using the gem called "rails3-jquery-autocomplete" ( https://github.com/crowdint/rails3-jquery-autocomplete )
It's working fine:) But there's one thing that I want to disable.
When I type something into an input field, and there's no matching record, it always show the message that's saying "no existing match"
I want to disable this message. Is it possible?
I wish I could see inside of its source code to customize but I cannot :(
Anyone can help me about this?
This might be piece of cake for someone but not for me.
For your information, I've done this command below
$ bundle exec rake assets:precompile RAILS_ENV=production
and after this it created some js files into public/assets folder

Seems there is no way to pass option into the control, but I assume this might be a trick to solve your problem.
Instead of returning empty array(from backend) when no search results, try to return a array with one result has empty id and label might solve your problem, so in your rails controller, there will be something like this:
def autocomplete_action
result = do_the_search
if result.count == 0
result = [{ id: "", label: "" }] # !! do the trick here
end
respond_with result # assume you have "respond_to :json" in your controller
end
Here is why I am doing this:
The reason why I think this might work is because if no result return, the autocomplete control will build a fake record with id equals to empty represent no result.
if(arguments[0].length == 0) {
arguments[0] = []
arguments[0][0] = { id: "", label: "no existing match" } // hacked here
}
But I do think this definitely not the right way to solve this problem, but for now it's workaround to solve your problem quickly.
Hope it helps, thanks.

Related

php str_replace produces strange results

I am trying to replace some characters in a text block. All of the replacements are working except the one at the beginning of the string variable.
The text block contains:
[FIRST_NAME] [LAST_NAME], This message is to inform you that...
The variables are defined as:
$fname = "John";
$lname = "Doe";
$messagebody = str_replace('[FIRST_NAME]',$fname,$messagebody);
$messagebody = str_replace('[LAST_NAME]',$lname,$messagebody);
The result I get is:
[FIRST_NAME] Doe, This message is to inform you that...
Regardless of which tag I put first or how the syntax is {TAG} $$TAG or [TAG], the first one never gets replaced.
Can anyone tell me why and how to fix this?
Thanks
Until someone can provide me with an explanation for why this is happening, the workaround is to put a string in front and then remove it afterward:
$messagebody = 'START:'.$messagebody;
do what you need to do
$messagebody = substr($messagebody,6);
I believe it must have something to do with the fact that a string starts at position 0 and that maybe the str_replace function starts to look at position 1.

set event listener that executes only on create

I have a coffee function that publishes to PubSub and also appends created element to a list here it is
handle_comments = ->
commentId = $('.question_comments').data('commentId')
PrivatePub.subscribe commentId, (data, channel) ->
res = $.parseJSON(data.comment_res.comment)
result = JST["templates/create_comment"]
user_name: data.comment_res.user
comment_body: res.body
comment_id: res.id
$('.question_errors').html("")
$('.comments_list').append(result)
$('.new_comment').find('#comment_body').val('')
$(document).ready(handle_comments)||$(document).on('page:load', handle_comments)||$(document).on('page:update', handle_comments)
Now I have a problem that this function executes every time I'm changing smth on a page( like deleting a node or else). I need a way to set it executing only when creating a comment.
p.s. handlers ready,page:load and page:update are set to avoid problems with turbolinks. Sorry for my poor English. Would be grateful for every single advice
The simplest way out I found, is to set a flag in controller action
PrivatePub.publish_to "/questions/#{#commentable.id}/comments", comment_res: { comment: #comment.to_json, user: current_user.name, **method: self.action_name** }
or even less complex executable: true (a bit similar to remote: true :) )
If you do know some other variants, I would be very curious to see them

Create keybinding to focus master client in awesome-wm

I would like to create a keybinding to switch focus to the master client. Profjim on this forum thread notes:
To get the master client on the current tag:
c = awful.client.getmaster()
I have tried the following, but it causes my ~/.config/rc.lua file to be ignored, which is the behavior if there is an error in the file. Does anyone know the correct syntax?
awful.key({ modkey, , "e", awful.client.getMaster()),
Note: "e" shouldn't cause any conflicts if you have the default key bindings.
Edit: Someone on /r/awesomewm knew the syntax to solve my problem:
awful.key({ modkey, }, "e", function() client.focus = awful.client.getmaster(); client.focus:raise() end),
Lets start with the syntax errors; from the documentation it seems that awful.key is a table, not a function. and it would presumably contain keys...which are hash tables, not sequences.
Finally your table syntax is wrong; a field may not be syntactically empty, it must have a listed value, even if that value is nil.
So basically you are trying to pass the wrong kind of value to something that can't be called.
As for how to do it correctly...the documentation is confusing, and apparently I'm not the only one that thinks so.
*deep breath*
okay, awful.new(...) creates key binders(?), and awful.key contains key bindings, so clearly we have to put the results of the first into the second.
the code on your link is but a pointer, and only covers focusing the window, not creating a keybinding.
It seems like you want something like this:
function do_focus()
current = client.focus
master = awful.client.getmaster()
if current then
client.focus = master
master:raise()
end
end
table.insert(awful.key, awful.new (modkey, 'e', nil, do_focus) )
Bare in mind that I have no way of testing the above code.

Error trying to Find_by_id, an extra value appears (Ruby on Rails)

I have a POST method that return this:
Request Parameters:
{"cm_test_ids"=>["1",
"8",
"9",
"10",
"11"],
"commit"=>"Create",
"authenticity_token"=>"WiBZQcZt2/Vi2RiFdFtaXnthClLsMubXe6sAhUzOPo8=",
"id"=>"1",
"cm_test_campaigns_object"=>{"cm_test_campaign_id"=>"1",
"comments"=>"",
"assigned_to_id"=>"6"}}
In my controller app I get the "cm_test_ids" like:
#ids_selected = params[:cm_test_ids]
In this point I have no problem, I'm able to save these ids (1,8,9,10,11) in a relation table but if I try to do this:
#my_tests = CmTest.find(["id IN (?)", #ids_selected])
I get this error:
"Couldn't find all CmTests with IDs (0,1,8,9,10,11) (found 5 results, but was looking for 6)"
The value "0" it doens't exits, I've print the array #ids_selected and I dont see it, I dont know why always in the IDs array is added a "0" in the first position, any idea?
You can try this:
CmTest.find_all_by_id(#ids_selected)
Rails generates a hidden field before every multi-select:
https://github.com/rails/rails/issues/5402
It looks like the blank string is being cast to an integer somewhere, which would give you a zero. You might need to strip this out of your parameters.
The error is "solved" using
CmTest.find_all_by_id(#ids_selected)
but find still return nil, I think I found the problem, the query generated have a lot of "\n" :
SELECT * FROM `cm_tests` WHERE (`cm_tests`.`id` IN ('id IN (?)','--- \n- \"1\"\n- \"8\"\n- \"9\"\n- \"10\"\n- \"11\"\n'))
You may also try
CmTest.where(id: #ids_selected )

Rails: Where clause doesn't work in any condition

I'm about to lose my mind due to a simple Rails where query. I simply cannot understand why it does work like 10 lines ago and does not after it. I could not figure out what might be causing the problem
#userID = Token.where(:tokenCode => #tokenReceived)
##init.tokenCode=#tokenReceived+"1" #randomize algorithm required!
#init.tokenCode=#codeGenerated=generate_activation_code()
if #userID.nil?
#newToken=Token.new
#newToken.tokenCode=#codeGenerated
else
#tokenToAdd = "12"
#newToken=Token.where(:userID => "1")
#if #newToken.nil?
#newToken.tokenCode="12"
#end
end
##newToken.save
#init.save
When I make a successful JSON request to 'http://localhost:3000/inits.json' it gives me a page with tons of erros but I think the main error among those are:
<h1>
NoMethodError
in InitsController#create
</h1>
<pre>undefined method `tokenCode=' for #<ActiveRecord::Relation:0x007fc43cb40b88></pre>
What could be the reason? Am I writing the where clause all wrong?
Edit: When I activate the if clause it works. I simply believe the #newToken object is null, however it is almost impossible for me to detect why. There is a data in my Token table with userID 1.
When you do:
#newToken=Token.where(:userID => "1")
You get an ActiveRecord::Relation, but you expect an object. So simply replace it with:
#newToken=Token.where(:userID => "1").first

Resources