Search code examples
formstwitterruby-on-rails-4save

Submitting follower by form


Getting strange error in rails when saving a follower:

Error on Page after submitting follower form

enter image description here

I execute all of the commands in the followers#create method in the rails console and it works. But for some reason when I execute the form on the page, it does not seem to save the user then I get this error on the page. Any ideas? Not sure if it's got something to do with my routes, so I'll post them as well.

Routes.rb:

TwitterApp::Application.routes.draw do
    get "followers/show"
    get "followers/new"
    get "dashboard/new"
    get "users/new"
    get "users/edit"
    get "sessions/new"
    get "add_follower" => "followers#new", :as => "add_follower"
    #get '/followers/:id', to: 'followers#show', as: 'followers'

    get '/like/:id', to: 'followers#like', as: 'like'
    get "log_out" => "sessions#destroy", :as => "log_out"
    get "" => "sessions#new", :as => "log_in"
    get "sign_up" => "users#new", :as => "sign_up"
    get "settings" => "users#edit", :as => "settings"
    get "dashboard" => "dashboard#new", :as => "dashboard"
    root :to => "sessions#new"
    resources :users
    resources :sessions
    resources :followers
end

Follower Form:

<%= @follower_to_save.inspect %>
                        <h3>Add Follower: </h3>
                        <%= form_for @follower do |f| %>
                            <% if @follower.errors.any? %>
                                <div class="error_messages">
                                    <h2>Form is invalid</h2>
                                    <ul>
                                        <% for message in @user.errors.full_messages %>
                                            <li><%= message %></li>
                                        <% end %>
                                    </ul>
                                </div>
                            <% end %>
                            <%= f.text_field :follower_username, class: "form-control", autofocus: "", required: "", placeholder: "Twitter Username" %><br/>
                            <p class="button"><%= submit_tag "Add", class: "btn btn-lg btn-primary btn-block" %></p>
                        <% end %>

Follower Controller:

class FollowersController < ApplicationController

    helper_method :logged_in?
    def new
        @follower = Follower.new
    end
    def create
        Twitter.configure do |config|

            # Test Account
            config.consumer_key = "6xkis3S0lpGZD6uFuhaLQ"
            config.consumer_secret = "pxxDbmipacThWNZYOYVphakXbXGa5IMY0k6CFoe0M"
            config.oauth_token = "2182147123-5V5Wy6syh420U6M6SduBWScZXiPSfCrLmN9GtUi"
            config.oauth_token_secret = "KgeKQEt8Tddq8xjSVZIQ65TW9m48GWJardgZA7zloGvDF"

            # Personal Account
            #config.consumer_key = "HwJH0RTuonm8Z01IwCr0pA"
            #config.consumer_secret = "1INqGLyDucW6rIHKKqq2pBS9pHgoIwQTg8CYvBuXrjI"
            #config.oauth_token = "82839887-bCld1xMgYKq9Bqe8ie4XhzcEevsOyNc7kJC3NOpOB"
            #config.oauth_token_secret = "In3a5B1oRG2xoaQSqoCk7gJqsJTOnMegWZoAirZp0tPzj"
        end

        @user = User.find(session[:user_id])
        @follower_to_save = Follower.new
        follower = Twitter.user(params[:follower_username])
        @follower_to_save.follower_id = follower[:id]
        @follower_to_save.owner = @user.id
        @follower_to_save.follower_username = follower[:screen_name]
        @follower_to_save.follower_nationality = follower[:location]
        @follower_to_save.no_of_followers = follower[:followers_count]
        @follower_to_save.following= follower[:friends_count]
        @follower_to_save.no_of_tweets = follower[:statuses_count]
        @follower_to_save.profile_picture_url = follower[:profile_image_url]
        @follower_to_save.updated_at = Time.now

        if @follower_to_save.save
            redirect_to "/dashboard", @just_updated => "Follower Added!"
        else
            render "new"
        end
    end
    def like
        follower = Follower.find(params[:id])
        if follower.liked == false
            follower.liked = true
        else
            follower.liked = false
        end
        follower.save
    end
    def show
        @follower = params[:id]
        Twitter.configure do |config|

            # Test Account
            #config.consumer_key = "6xkis3S0lpGZD6uFuhaLQ"
            #config.consumer_secret = "pxxDbmipacThWNZYOYVphakXbXGa5IMY0k6CFoe0M"
            #config.oauth_token = "2182147123-5V5Wy6syh420U6M6SduBWScZXiPSfCrLmN9GtUi"
            #config.oauth_token_secret = "KgeKQEt8Tddq8xjSVZIQ65TW9m48GWJardgZA7zloGvDF"

            # Personal Account
            config.consumer_key = "HwJH0RTuonm8Z01IwCr0pA"
            config.consumer_secret = "1INqGLyDucW6rIHKKqq2pBS9pHgoIwQTg8CYvBuXrjI"
            config.oauth_token = "82839887-bCld1xMgYKq9Bqe8ie4XhzcEevsOyNc7kJC3NOpOB"
            config.oauth_token_secret = "In3a5B1oRG2xoaQSqoCk7gJqsJTOnMegWZoAirZp0tPzj"
        end
        @follower_tweets = Twitter.user_timeline(@follower)
        @follower_db_instance = Follower.find_by_follower_username(@follower)
        @follower_profile_pic ||= Twitter.user(@follower)[:profile_image_url]
    end
    def logged_in?
        if session[:user_id].present?
            true
        else
            false
        end
    end
end

Solution

  • rename @follower_to_save to @follower in method create