通过表单提交关注者

时间:2013-11-29 11:36:15

标签: forms twitter ruby-on-rails-4 save

保存关注者时,在rails中出现奇怪错误:

提交关注者表单后页面出错

enter image description here

我在rails控制台中的关注者#create方法中执行所有命令,它可以正常工作。但由于某些原因,当我在页面上执行表单时,它似乎没有保存用户,然后我在页面上收到此错误。有任何想法吗?不确定它是否与我的路线有关,所以我也会发布它们。

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_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 %>

追随者控制器:

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

1 个答案:

答案 0 :(得分:0)

在方法创建

中将@follower_to_save重命名为@follower