문제

I create a class that takes two input parameters from the user, these input parameters are then used to create five others, the problem is that the five others don't show up when I check my validated paramaters only the first ones.

I know that all of the new parameters are added to the params but they don't show up in the model time_delta_params hash when I check what's in it only the first two. Thanks for any help!

My create method for the controller

  def create
    # XXX Add these columns to the model and populate them
    # finalRating, positiveTweets, negativeTweets, neutralTweets, totalTweets
    tweetRatings = {:finalRating => 0, :positiveTweets => 0, :negativeTweets => 0, :neutralTweets => 0} 
    @stock = Stock.find(params[:stock_id])
    tweets = getTweets(@stock.hashtag, time_delta_params[:start], time_delta_params[:length].to_i)
    tweets.each do |tweet|
      case processTweet(tweet)
        when 1
          tweetRatings[:positiveTweets]  += 1
          tweetRatings[:finalRating]     += 1
        when -1
          tweetRatings[:negativeTweets]  += 1
          tweetRatings[:finalRating]     -= 1
        else 
          tweetRatings[:neutralTweets]   += 1
      end
    end

    params[:final]      = tweetRatings[:finalRating]
    params[:positive]   = tweetRatings[:positiveTweets]
    params[:negative]   = tweetRatings[:negativeTweets]
    params[:neutral]    = tweetRatings[:neutralTweets]
    params[:total]      = tweets.count
    # printSomthingToRender(time_delta_params)
    @time_delta = @stock.time_deltas.create(time_delta_params)
    redirect_to stock_path(@stock)
  end

My validation:

def time_delta_params
      params.require(:time_delta).permit(
        :start, 
        :length,
        :final, 
        :positive, 
        :negative, 
        :neutral, 
        :total
        )
    end
도움이 되었습니까?

해결책 2

You're calling create with the time_delta_params that isn't going to contain the tweetRatings data. You would need to do something like params['time_delta'][:final] = tweetRating[:finalRating]. You could also call create and create your hash there or rename the values in the tweetRatings hash to match what is in the model.

다른 팁

You are not merging the additional parameters into the time_delta hash, but straight to the top level of params. time_delta is a hash within params.

You need to do something like:

params[:time_delta].merge!(final:    tweetRatings[:finalRating],
                           positive: tweetRatings[:positiveTweets],
                           negative: tweetRatings[:negativeTweets],
                           neutral:  tweetRatings[:neutralTweets],
                           total:    tweets.count)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top