0

所以我试图在创建记录之前验证我通过的参数。我正在模型中尝试这个。

模型

 def self.create_hashtag_signed_in(hashtag)
  hashtag _regex = /[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/i

  validates hashtag :presence   => true,
                    :format     => { :with => hashtag_regex },
                    :uniqueness => { :case_sensitive => false }    

  dash = "#"
  # @view_count_init = "0"
  @hashtag_scrubbed = [dash, hashtag].join
  User.current_user.twitter.search("%#{@hashtag_scrubbed}", :lang => "en", :count => 20, :result_type => "mixed").results.map do |tweet|
    unless exists?(tweet_id: tweet.id)
        create!(
            tweet_id: tweet.id,
            text: tweet.text,
            profile_image_url: tweet.user.profile_image_url,
            from_user: tweet.from_user,
        from_user_name: tweet.user.name, 
            created_at: tweet.created_at,
        hashtag: @hashtag_scrubbed,
        view_count: "0",
        wins: "0"
          ) 
        end     
    end
  end

控制器

def create 
            @stats_total = Hashtag.count 
    @stats_wins = Hashtag.sum(:wins)
    @stats_views = Hashtag.sum(:view_count)

    @stats_losers = (@stats_views - @stats_wins) 
        @vote_history = Hashlog.vote_history
        if signed_in?
            Hashtag.create_hashtag_signed_in(params[:hashtag])
        else
            Hashtag.create_hashtag_guest(params[:hashtag])
        end
        Hashlog.create_hashlog(params[:hashtag])
        @random_hashtag_pull = Hashtag.random_hashtags_pull
        @leaderboard = Hashtag.leaderboard_history_current
        respond_to do |format|
        format.html { redirect_to root_path }
        format.js
    end
     end

它正在抛出错误

Started GET "/" for 127.0.0.1 at 2012-12-11 23:52:57 -0800

NoMethodError (undefined method `key?' for nil:NilClass):

然后,如果它抛出不存在,我希望能够通过 jQuery 将其返回给提交是如何工作的。

更改为 :hashtag 错误

/Users/user/Development/tweetvstweet/app/models/hashtag.rb:34: syntax error, unexpected ':', expecting keyword_end
validates :hashtag :presence => true, 
                    ^
/Users/user/Development/tweetvstweet/app/models/hashtag.rb:34: Can't assign to true
validates :hashtag :presence => true, 
                                     ^
/Users/user/Development/tweetvstweet/app/models/hashtag.rb:35: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
          :format => { :with => hashtag_regex }, 
                    ^
/Users/user/Development/tweetvstweet/app/models/hashtag.rb:35: syntax error, unexpected ',', expecting keyword_end
          :format => { :with => hashtag_regex }, 
                                                ^
4

1 回答 1

0

在你的模型中添加这样的然后尝试它可能会起作用

  def self.create_hashtag_signed_in(hashtag_value)
     hashtag _regex = /[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/i

     validates :hashtag , :presence   => true,
                  :format     => { :with => hashtag_regex },
                  :uniqueness => { :case_sensitive => false }    

dash = "#"
# @view_count_init = "0"
@hashtag_scrubbed = [dash, hashtag_value].join
User.current_user.twitter.search("%#{@hashtag_scrubbed}", :lang => "en", :count => 20, :result_type => "mixed").results.map do |tweet|
  unless exists?(tweet_id: tweet.id)
      self.create!(
          tweet_id: tweet.id,
          text: tweet.text,
          profile_image_url: tweet.user.profile_image_url,
          from_user: tweet.from_user,
      from_user_name: tweet.user.name, 
          created_at: tweet.created_at,
      hashtag: @hashtag_scrubbed,
      view_count: "0",
      wins: "0"
        ) 
   end     
 end

结尾

试试可能是......

于 2012-12-12T08:18:29.093 回答