2012-12-12 30 views
0

所以我想在創建記錄之前驗證我的param通過。我正在試圖在模型中。在創建之前驗證正則表達式和存在

型號

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): 

然後,如果它拋出的沒有存在我希望能夠返回它對v ia jQuery提交如何工作。

更改爲:#標籤錯誤

/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 }, 
               ^
+0

請寫下以下內容: hashtag_regex =/[A-Za-z] [A-Za-z0-9] *(?:_ [A-Za-z0-9] +)* $/i validates:hashtag:presence => true, :格式=> {:with => hashtag_regex}, :唯一性=> {:case_sensitive => false} – VenkatK

+0

我更新了帖子,錯誤號爲 – thebusiness11

+0

validates:hashtag,:presence => true,:format => {使用=> hashtag_regex}::uniqueness => {:case_sensitive => false} – VenkatK

回答

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 

嘗試可能.... 。

+0

Error'ActiveRecord :: RecordInvalid(Validation failed:Hashtag is invalid,Hashtag has alr ): app/models/hashtag.rb:86:in'block in create_hashtag_guest' app/models/hashtag.rb:75:in'map' app/models/hashtag.rb:75:in' create_hashtag_guest' app/controllers/hashtags_controller.rb:35:在'create'' – thebusiness11

+0

檢查您的模型字段名稱和類型。在控制檯中手動創建查詢。 –