从rails 4中的number_field_tag获取值

时间:2013-11-20 05:32:04

标签: ruby-on-rails forms

我试图获取用户在数字字段标记中设置的值,并使用按钮将其传递给控制器​​。以下是我到目前为止的情况:

<% if current_user && current_user.can_vote_for?(sound) %>
  <%= number_field_tag 'rating', nil, in: 1...4 %>
  <%= button_to "Rate", rate_sound_path(sound, value: params[:rating]), method: "post" %>
<% end %>

在声音控制器中:

  # Give the sound a rating
  def rate
    rating = current_user.sound_ratings.new(sound_id: params[:id], value: params[:value])
    if rating.save
      redirect_to '/sounds', notice: "Thank you for voting."
    else
      redirect_to '/sounds', alert: "Unable to vote, perhaps you already did."
  end
 end

不确定出了什么问题。我知道sound_ratings表没有使用新的评级进行更新。

不确定这是否有用,但这里是sound_rating模型:

class SoundRating < ActiveRecord::Base

  belongs_to :sound
  belongs_to :user

  validates_uniqueness_of :sound_id, scope: :user_id
  validates_inclusion_of :value, in: [1, 4]
  validate :ensure_not_author

  def ensure_not_author
    errors.add :user_id, "is the author of the sound" if sound.user_id == user_id
  end
end

任何提示?

0 个答案:

没有答案