JSON::ParserError (399: unexpected token at - ruby

when I'm going to parse this:
Product = [{"Productname":"Acer 18.5" LED Monitor}]
with Ruby JSOn, its showing me the error JSON::ParserError (399: unexpected token at. I know this is for 18.5" in the string. How can I parse this string?

You have to give the string like this
Product = [{"productname":%q{"Acer 18.5" LED Monitor}}]

If you have an issue with Mongoid + Devise add this to your User model (bug page):
def to_key
id.to_s
end

In your /app/views/layouts/application.html.erb lines 5 and 6, change the first parameter from application to default.
I met the same problem, too for my situation, after changing it the problem was resolved happens on Windows. The parameter application works on the web server.

Related

Ruby how to call a class setter with yield

I was able to get my code working, but I don't like the way it works syntaxually - not sure if that is even a word.
My code currently:
user.set_phone(phone) do
message = "Verify your phone number #{#host}/phone/verify?id=#{user.id}&code=#{user.phone_verification_code}"
CoreIntegrations::Sinch.send_text(message, phone)
end
How I would like it to look/work:
user.phone=(phone) do
message = "Verify your phone number #{#host}/phone/verify?id=#{user.id}&code=#{user.phone_verification_code}"
CoreIntegrations::Sinch.send_text(message, phone)
end
This is the Error I'm getting
/Users/christianjuth/Desktop/apollo-8/app/controllers/application_controller.rb:140: syntax error, unexpected keyword_do, expecting keyword_end /Users/christianjuth/Desktop/apollo-8/app/controllers/application_controller.rb:267: syntax error, unexpected keyword_end, expecting end-of-input
The line number might in sync with the repo, case I have some local changes made to the application controller.
...Or something along those lines. user is an ActiveRecord model. The reason I want to do this, is yield only gets called if the variable phone sent into user.phone= is different from the existing value of user.phone.
I think this is more of a syntax issue, but just in case here is repo for context. The code you are looking at lives in app/controllers/application_controller.rb, and the user model can be found in user/models/user.rb.
UPDATE: I am having trouble calling the setter with yield not defining it. Sorry for the confusion.
If you change the method from def set_phone to def phone= then will be more difficult to invoke, but still possible:
user.send :phone=, phone do
# ...
end

Unable to read cookie in Rspec 3 via last_response

I am trying to read in Rspec 3.1 a cookie received after get call.
I see it is returned but the last_response.cookies doesn't exist.
How can I read response's cookie?
it "doesn't signs in" do
get '/ui/pages/Home'
puts last_response.cookies
end
I know it has been a while, but facing exactly this same issue now, after some struggle, I've found an article here that shares an interesting approach. As I also couldn't find any native parsed method for this, that has worked fine for me.
Basically, place this piece of code below on your spec/spec_helper.rb:
def cookies_from_response(response=last_response)
Hash[response["Set-Cookie"].lines.map { |line|
cookie = Rack::Test::Cookie.new(line.chomp)
[cookie.name, cookie]
}]
end
and you could use this to see the parsed hash:
puts cookies_from_response
For a cookie's value check, you could then use something like:
# Given your cookie name is 'foo' and the content is 'bar'
expect(cookies['foo'].value).to eq 'bar'
Hopefully this becomes helpful to others facing similar issues.

Parameter from the path includes query string in Sinatra. Is that correct?

I am running a Sinatra app under Passenger. I have an action which looks roughly like this:
get '/pic/:id' do
# do stuff ...
canonical_image_url = "/img/%d.jpg" % params[:id]
end
However I see my app is failing with the following exception
ArgumentError (invalid value for Integer(): "22?fill=width&width=512&sig=173798632b6ce659234a34c05324196c92b9a8ef")
which means that somehow the QS parameters are not being extracted from the path. Is this some kind of a weird escaping problem? (that some part of my app requests with a double-encoded query string) or is this a known problem? Or is it designed that way and path-params and QS params cannot be used at the same time?
A simpler way to write this (which will probably not help solve your problem, but is too long for a comment):
get '/pic/:id' do |id|
# do stuff ...
canonical_image_url = "/img/%d.jpg" % id
end

Mongoid has_and_belongs_to_many associations

I trying to get mongoid to save associations, but I can only get one side to work. If I have the following test.
test "should add a user as a follower when a user follows the group" do
#cali_group.followers = []
#user1.followed_groups << #cali_group
assert_equal 1, #user1.followed_groups.count
assert_equal 1, #cali_group.followers.count
end
Which is failing, because #cali_group.followers is []. I've been working with this for awhile, tried #cali_group.reload. But it looks like the only way to do this in my code is to work both ends of the join, i.e. #cali_group.followers << #user1. I can do that in my code if I have to.
The models for polco_group and user are here: https://gist.github.com/1195048
Full test code is here: https://gist.github.com/1195052
It can be that:
https://github.com/mongoid/mongoid/issues/1204
Very late to the show. Using Mongoid 4.0.2 here. The issue is troubling me as well.
The link by #sandrew is no longer valid. A similar issue was reported here: http://github.com/mongodb/mongoid/pull/3604
The workaround that I found was:
#cali_group.followers = []
#cali_group.follower_ids # Adding this line somehow does something to the cache
#user1.followed_groups << #cali_group
Found this workaround by adding a before_save in the Group class and observing self.changes. Without this line, the follower_ids member changes from nil to []. However after adding the line, the correct ID of the user is received and set. Hope that helps any future reader.

Rails: ParameterFilter::compiled_filter tries to dup symbol

I'm running rails3 with rails exception-notifier gem. When an exception occurs, and an email should be sent, I'm getting an exception from the ParameterFilter class. I've found the problem in the rails source, and am not sure the best way to proceed.
The problem occurs in ActionDispatch::Http::ParameterFilter. In the compiled_filter method, an error occurs on line 38: key = key.dup when key is a symbol, because symbols are not duplicable. Here is the source:
def compiled_filter
...
elsif blocks.present?
key = key.dup
value = value.dup if value.duplicable?
blocks.each { |b| b.call(key, value) }
end
I see that they only call dup on value when it is duplicable. If I patch the source to only call dup on key when key is duplicable, then my problem goes away. I'm assuming there is a reason why the author put that condition on value and not key, so I'm curious if someone out there has a better understanding of this code.
This error only occurs when you add a block to your filter params in application.rb. So, maybe there is a workaround for my original issue that does not require using a block here. If you're interested see my coworker's question Rails: Filter sensitive data in JSON parameter from logs
The key for which this is a problem is :action. This comes from rails and I don't know if there is any way to force it to be a string instead.
I filed a rails bug https://rails.lighthouseapp.com/projects/8994/tickets/6557-symbol-duplication-error-in-parameterfilter-compiled_filter and I have a patch ready that adds if key.duplicable? to the key.dup line, I'm looking for input on whether or not that is the right solution.
This looks like a bug in Rails. Either the key should be a string rather than a symbol, or the dup should be protected by duplicable?.
You should file a bug at https://rails.lighthouseapp.com/, including a minimal test case if possible.

Resources