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

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)

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.

Woocommerce - Change order received text after redirecting from PayPal with IPN

I like to change the thank you page text in case the customer already paid via paypal.
Redirecting via PayPal works fine. Order status is "processing", ok.
But how can I change the thank you text on the redirecting page (https://www.example.com/checkout/order-received/)
I tried the following, with no success:
function my_update_order_status() {
$order = new WC_Order($order_id);
if ($order_status == 'processing')
echo 'NEW MESSAGE';
else
echo 'NOT PAID TEXT';
}
add_filter('woocommerce_thankyou_order_received_text', 'my_update_order_status', 10, 2);
There is an explanation on how to change the thank you page here:
https://wordpress.org/support/topic/how-to-edit-the-thank-youorder-received-page
If you don't wish to do any template modifying then you can include the order received function to adjust it. For example:
function isa_order_received_text( $text, $order ) {
$new = $text . ' All received and an email is on its way to you!.';
return $new;
}
add_filter('woocommerce_thankyou_order_received_text', 'isa_order_received_text', 10, 2 );
The above with add text to the current text output.

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

databasedotcom error on no record found

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

Facebook infinite redirect loop during authentication

I am getting an infinite loop in the URL redirect after a user either logs in or is already logged in. The page redirects to the login page if the user is not logged in as expected , but goes in the loop as soon as he enters the credentials.
Following is the code:
<?php
include_once ('facebook.php');
$api_key = 'xxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxx';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
$uid = $facebook->require_login($required_permissions = 'email,status_update,offline_access');
$facebook->api_client->users_hasAppPermission("offline_access",$uid);
#echo $uid;
# $facebook->api_client->users_setStatus("hello",$uid);
# echo "<p>Your status has been updated</p>";
?>
Interestingly this code was working before, but suddenly started giving me an infinite loop problem. There have been couple of discussion on facebook forum about this but no indication that is a bug or what is the workaround .
Any help would be highly appreciateed.
Thanks
I added the code to direct to login only if the user is not logged in else do not do that. This worked for me! .. Hope it helps.
$is_tab = isset($_POST['fb_sig_in_profile_tab']);
if( !$is_tab ){
$uid = $facebook->require_login($required_permissions = 'email,status_update ,offline_access');
}
else{
$uid = $facebook->get_profile_user();
}