Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: Create a button if a row exists in db
    primarykey
    data
    text
    <p>I'm creating a twitter-copy (just to learn Rails). I have a users-page where all users are listed. Next to the username I want to create a button that says "follow" if you are not following this user and "unfollow" if you are.</p> <p>I have already done it in PHP and I thought i could do something similar in Rails, but I'm not sure how.</p> <p><strong>followBtn.php</strong> - included after every user.</p> <pre><code>$subsTable = new Table('Sub'); if($user != $currentUser) { $follows = $subsTable-&gt;checkFollow($currentUserID, $userID); if($follows) { echo "&lt;form action='src/php/main.php' autocomplete='on' method='post'&gt;"; echo "&lt;input type='hidden' name='page' value='unfollow'&gt;"; echo "&lt;input type='hidden' name='followID' value='$userID'&gt;"; echo "&lt;input type='submit' value='Unfollow' id='unfollowBtn'&gt;"; echo "&lt;/form&gt;"; } if(!$follows) { echo "&lt;form action='src/php/main.php' autocomplete='on' method='post'&gt;"; echo "&lt;input type='hidden' name='page' value='follow'&gt;"; echo "&lt;input type='hidden' name='followID' value='$userID'&gt;"; echo "&lt;input type='submit' value='Follow' id='followBtn'&gt;"; echo "&lt;/form&gt;"; } } </code></pre> <p><strong>Table.php</strong></p> <pre><code>public function checkFollow($user, $follower) { $table = (string)self::$table; $query = " SELECT * FROM $table WHERE $table.userID = '$user' AND $table.followID = '$follower' ;"; $database = Database::getInstance(); $result = $database-&gt;fetch_assoc($query); return $result; } </code></pre> <p>This is my Rails-code I've got so far. I know it's not working, but maybe it's not that much that is wrong.</p> <p><strong>index.html.erb</strong></p> <pre><code>&lt;h2&gt;All users&lt;/h2&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Username&lt;/th&gt; &lt;th&gt;Email&lt;/th&gt; &lt;/tr&gt; &lt;% @user.each do |u| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= u.username %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= u.email %&gt;&lt;/td&gt; &lt;% if(u.username != @current_user.username) %&gt; &lt;% if @following %&gt; &lt;% @temp_user = u %&gt; &lt;%= form_for(:subscription, :url =&gt; { :controller =&gt; "subscriptions", :action =&gt; "unsubscribe" }) do |s| %&gt; &lt;%= s.hidden_field(:userID, :value =&gt; @current_user.id) %&gt; &lt;%= s.hidden_field(:followingID, :value =&gt; u.id) %&gt; &lt;td&gt;&lt;%= s.submit "Unsubscribe" %&gt;&lt;/td&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;%= form_for(:subscription, :url =&gt; { :controller =&gt; "subscriptions", :action =&gt; "subscribe" }) do |s| %&gt; &lt;%= s.hidden_field(:userID, :value =&gt; @current_user.id) %&gt; &lt;%= s.hidden_field(:followingID, :value =&gt; u.id) %&gt; &lt;td&gt;&lt;%= s.submit "Subscribe" %&gt;&lt;/td&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; </code></pre> <p><strong>user_controller.rb</strong></p> <pre><code>class UsersController &lt; ApplicationController before_filter :save_login_state, :only =&gt; [:new, :create] def index @user = User.all @current_user = User.find session[:user_id] @following = true if Subscriptions.find_by_sql("SELECT * FROM subscriptions WHERE userID = '{@current_user.id}' AND followingID = '{@temp_user.id}';") end end </code></pre> <p><strong>Errors</strong></p> <pre><code>NameError in UsersController#index uninitialized constant UsersController::Subscriptions </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload