2

我正在尝试在我的应用程序中实现评级系统,我尝试了 Rateit 但无法让它工作,所以我想我会尝试构建自己的,而且我希望通过了解这个过程来学习更多

目前我正在尝试传递点击星的值

形式

<%= form_for @rating do |f| %>
  <%= f.hidden_field :ratings, :id => "hiddenRating", :value => '' %>
  <%= f.hidden_field :user_id, :value => current_user.id %>
  <%= f.hidden_field :recipe_id, :value => @recipe.id %>
  <div class="ratings">
    <ul>
      <li id="firstStar"></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
  <%= f.submit "Submit" %>
<% end %>

JS

$(document).ready(function(){
  $('#firstStar').click(function(){
    $('#hiddenRating').value = 1;
  });
}); 

所以想法是,如果用户单击第一个星,则应将值 1 作为表单中的评级值传递,这不会发生,因为我不知道在其中传递什么

:value => ''

我相信有更好的方法可以做到这一点,但正如我所说,我想一点一点地学习,以便到最后我可以把它们放在一起,当然如果有人有更好的建议,请告诉我。

编辑

控制器

def new
  @rating = Rating.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @rating }
  end
end

def create
  @rating = Rating.new(params[:rating])

  respond_to do |format|
    if @rating.save
      format.html { redirect_to @rating, notice: 'Rating was successfully created.' }
      format.json { render json: @rating, status: :created, location: @rating }
    else
      format.html { render action: "new" }
      format.json { render json: @rating.errors, status: :unprocessable_entity }
    end
  end
end
4

1 回答 1

2

好的,希望这可以帮助处于类似情况的其他人,我的表格现在看起来像这样

<%= f.hidden_field :ratings, :id => "hiddenRating"%>#Value has been removed

我的 Jquery 看起来像这样

$(document).ready(function(){
 $('#firstStar').click(function(){
  $('#hiddenRating').val(1);
 });
});

无需在表单中传递值,因为 .val() 将其分配给 id

反正这是我的理解

于 2013-02-20T13:43:52.263 回答