databasedotcom error on no record found - databasedotcom-gem

Just started using databasedotcom gem and now stuck.
When trying to find a record in Salesforce by Id, if the id/record doesn't exist, it produces the following error:
"Provided external ID field does not exist or is not accessible:"
If the Id does exist I am able to continue with my page fine.
Here's my code:
def search
#account = Account.find(params[:id])
if #account.Id
redirect_to new_user_registration_path(:id => #account.Id)
elsif params[:id].to_s.present? and #account.nil?
flash[:alert] = "Couldn't find that one. Please check and re-submit your number."
redirect_to search_path
end
end
How can I overcome this?
Thank you.

Changed the code to the following and it works fine now - thank you Danny Burkes. It captures the exception and routes accordingly.
def search
begin
#account = Account.find(params[:id])
if #account.Id
redirect_to new_user_registration_path(:id => #account.Id)
end
rescue Exception => e
flash[:alert] = "Couldn't find that one. Please check and re-submit your number."
redirect_to search_path
end
end

Related

Integration of myBB with website

I want my website and my mybb board to be connected. I Googled and made the integration, and I'm able to get a username id, as long as I specify their ID, but nothing happens when I'm using the function as explained.
What I'm trying to do is verify a connection of one to the forums, via my website, and then show their name.
Here's my code, as well as my attempts to get a username:
PHP Code:
define('IN_MYBB', NULL);
require_once 'forums/global.php';
require_once 'forums/class.MyBBIntegrator.php';
$MyBBI = new MyBBIntegrator($mybb, $db, $cache, $plugins, $lang, $config);
$user = get_user($uid);
while ($forum_user = mysqli_fetch_array($user))
{
echo $forum_user['username'];
}
You could use:
define('IN_MYBB', NULL);
require_once 'forums/global.php';
if(isset($mybb->user['uid'])){
echo "User Found: ".$mybb->user['usernmae']."($mybb->user['uid'].")";
} else {
echo "No user found";
}
MyBBIntegrator isn't even required. But the above code will check if the user is logged in, and display the username and userid. If not it will display No User Found. Hopefully this helps.

Separate logins with different privileges using switch case in php 5.5

I'm having some trouble with a block of code I'm trying to implement.
My thought process was to have different users with different attributes in the "userType" (which is an INT type) column in my database and to have a switch case to take the result of the query and select which page to redirect to. The links for re-direction haven't been inserted yet as the login switch case isn't working.
Here is the code for my "loginsuccess.php"
<?php
session_start(); // Right at the top of your script
?>
<?php
ob_start();
$host="localhost"; // Host name
$username="google_test"; // Mysql username
$password="password"; // Mysql password
$db_name="google_test"; // Database name
$tbl_name="user"; // Table name
$mysqli = new mysqli($host, $username, $password, $db_name);
if($_SESSION['$myusername']==true)
{
$sessionUsername = $_SESSION['$myusername'];
$userType = "SELECT userType FROM '$tbl_name' WHERE username='$sessionUsername'";
$result = mysqli_query($mysqli, $userType);
switch ($result){
case "1":
//userType 1 is admin
echo $_SESSION['$myusername'];
echo ", Login Successful. Welcome Admin.";
break;
case "2":
//userType 2 is business
echo $_SESSION['$myusername'];
echo ", Login Successful. Welcome to your business page.";
break;
case "3":
//userType 3 is general user
echo $_SESSION['$myusername'];
echo ", Login Successful. Welcome to the Fun Finder App!!.";
break;
default:
echo $_SESSION['$myusername'];
echo ", it appears that your user type has not been defined yet.";
echo " Please contact support to resolve this issue.";
}
}
elseif($_SESSION['$myusername']==false)
{
echo "oops......";
//echo '<span>Login/Register</span></li>';
}
ob_end_flush();
?>
My login script is working and passes the username and password through the database successfully via another php page. The result I'm getting from this php file is that the switch case goes directly to the default case regardless of which login I use. I'm assuming it is something to do with my query not returning the proper result as I tried to echo the $result variable and got nothing. Php is not my strongest language so any help would be appreciated. Thanks in advance!
Mysqli_query returns a result object and not the actual value. You can read the value from that object however:
http://php.net/manual/en/mysqli.query.php
As an additional hint: it might make sense to store these user privileges along with the user login name in the session once the user succesfully authenticates. (There might be more places where this info is needed - that way you don't need to do queries all the time)
The only drawback is that this will not respond to permission chamgesnfor that user (when a user becomes admin he will need to login again)
In my opinion you must use mysql_fetch_array() to store the result from the database before using it in a switch case.
example:
$row=mysql_fetch_array($result);
$privilege=$row['usertype']; //user type is from the column in your table

RAILS 4 After editing and updating, the id of posts is passed as user why?

I have a user model that has_many posts, but I have few issues when a user updates a post. When a first user (user id:1) updates a post, (say post id:13), upon redirect, the page tries to redirect to user id:13. Does anyone know why this is? Here are my lines of code.. Thanks in advance.
posts_controller.rb
.
.
.
def update
#post = current_user.posts.find(params[:id])
if #post.update_attributes(post_params)
redirect_to user_path, notice: "Successfully updated!"
else
render action: "edit"
end
end
users_controller.rb
.
.
.
def show
#user = User.find(params[:id])
#posts = #user.posts.paginate(page: params[:page])
end
redirect_to **user_path**, notice: "Successfully updated!"
You are redirecting to the user_path. You want to redirect to the post_path. Unless what you want is directing to the user_path. Then, you need to pass it the user id: user_path(current_user.id)

Write Web call for iphone in ruby on rail. I want to get json formate and send response but formate of json is incorrect

I have a bug in the following Rails code which I cannot find. Any help would be appreciated.
class Api::ItemsController < ApplicationController
respond_to :json
def create
begin
#puts "params: #{params}"
if params[:item_post][:user][:uid] && params[:item_post][:user][:provider]
#user = User.find_by_provider_and_uid(params[:item_post][:user][:provider], params[:item_post][:user][:uid])
elsif params[:item_post][:user][:token]
#user = User.create_with_token(params[:item_post][:user][:token])
elsif params[:item_post][:user][:email]
#user = User.find_by_email(params[:item_post][:user][:email])
end
if #user
#item = #user.items.new(params[:item_post][:item])
else
#item = Item.new(params[:item_post][:item])
#item.reply_email = params[:item_post][:user][:email]
end
if #item.save
if params[:item_post][:item_images]
params[:item_post][:item_images].each_value do |item_image|
#item.images.create(item_image)
end
end
respond_with(#item)
else
#puts "Errors 1: #{#item.errors}"
respond_with(#item.errors)
end
rescue => e
#puts "Errors 2: #{e.message}"
respond_with(e.message.to_json, :status => :unprocessable_entity)
end
end
end
Problem:
I have been looking into the parameters received by the server. And I've tried to find the code where the parameters are built. Where can I find it?
In the server logs I can see the post as:
{"item_post":"{\"item\":{\"title\":\"Apple TV\",\"price\":\"45.50\",\"description\":\"Dual-Shielded High Speed HDMI Cable with Ethernet 2M are sold yesterday\",\"zipcode\":\"94102\"},\"user\":{\"email\":\"user#email.com\"}}}
I was expecting the post to look like this:
{\"item_post\":{\"item\":{\"title\":\"Apple TV\",\"price\":\"45.50\",\"description\":\"Dual-Shielded High Speed HDMI Cable with Ethernet 2M are sold yesterday\",\"zipcode\":\"94102\"},\"user\":{\"email\":\"user#email.com\"}}}
It seems to me that the 'item_post' block is being wrapped in unnecessary quotes, making it a string instead of a JSON hash:
I think this:
{"item_post":"{
should look like (no quote) this:
{"item_post":{
I am only guessing to what the problem could be.
I think you need avoid the to_json in your rescue
respond_with(e.message.to_json, :status => :unprocessable_entity)
put
respond_with(e.message, :status => :unprocessable_entity)
In your case the to_json before the to_json in the respond_with return a String it's why you have a String convert in JSON and not your Hash convert in JSON

How to allow custom flash keys in a redirect_to call in Rails 3

In Rails 3, you can pass has attributes directly to redirect_to to set the flash. For example:
redirect_to root_path, :notice => "Something was successful!"
However, this only works with the :alert and :notice keys; if you want to use custom keys, you have to use a more verbose version:
redirect_to root_path, :flash => { :error => "Something was successful!" }
Is there any way to make it so that custom keys (such as :error, above) can be passed to redirect_to without specifying it in :flash => {}?
In Rails 4 you can do this
class ApplicationController < ActionController::Base
add_flash_types :error, ...
and then somewhere
redirect_to root_path, error: 'Some error'
http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013
I used the following code, placed in lib/core_ext/rails/action_controller/flash.rb and loaded via an initializer (it's a rewrite of the built-in Rails code):
module ActionController
module Flash
extend ActiveSupport::Concern
included do
delegate :alert, :notice, :error, :to => "request.flash"
helper_method :alert, :notice, :error
end
protected
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
if alert = response_status_and_flash.delete(:alert)
flash[:alert] = alert
end
if notice = response_status_and_flash.delete(:notice)
flash[:notice] = notice
end
if error = response_status_and_flash.delete(:error)
flash[:error] = error
end
if other_flashes = response_status_and_flash.delete(:flash)
flash.update(other_flashes)
end
super(options, response_status_and_flash)
end
end
end
You can, of course, add more keys besides just :error; check the code at http://github.com/rails/rails/blob/ead93c/actionpack/lib/action_controller/metal/flash.rb to see how the function looked originally.