简体   繁体   English

向Rails模型提交价值

[英]Submit value to Rails Model

if i have this as my form 如果我有这个表格

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

How do i pass the value of the content within this div 如何在此div中传递内容的值

<div class="rateit" id="rateit10">
</div>

if i use this jquery I can get the value of rateit10 如果我使用此jQuery,我可以得到rateit10的值

$("#rateit10").bind('rated', function() { 
 $(this).rateit('value');
  });

The value derives from clicking upon a set of stars, im trying to implement a rating system in rails using the Rateit plugin 该值来自单击一组星号,即尝试使用Rateit插件在铁轨中实施评级系统

This will pass a hash to your controller method: 这会将哈希值传递给控制器​​方法:

   function pass_to_controller(){
    var rate_value = $("#ratings").value();
    var user_id = $("#user_id").value();  /*you already have access to this through current_user.id */
    var recipe_id = $("#recipe_id").value();

    $.ajax({
        type: "Get",
        url: "path_to_your_controller_action_here",
        timeout: 5000,
        data: {rating : rate_value, user : user_id, recipe : recipe_id }
    }).done(function(response){//do your response here});
}

You can't directly pass to the model - that circumvents the whole thought process behind MVC. 您不能直接传递给模型-绕开了MVC背后的整个思考过程。 So the best way to do it is to use ajax , pass it to your controller method, then if you need to do something in your database call your model method. 因此,最好的方法是使用ajax并将其传递给控制器​​方法,然后如果需要在数据库中执行某些操作,请调用模型方法。

The advantage of using ajax like this is that it won't reload your page when someone rates something. 像这样使用ajax的好处是,当有人评价某项内容时,它不会重新加载您的页面。

The easiest way is to trigger an update of your hidden field when the user click on a star, rather than read the value of the stars when the form is submitted. 最简单的方法是,当用户单击星号时触发隐藏字段的更新,而不是在提交表单时读取星号的值。

I should add a onnclick event on the stars to update the hidden field. 我应该在星星上添加onnclick事件以更新隐藏字段。 To do this, add an ID to your hidden field with :id => "my_hidden_field" to name it. 为此,请使用:id =>“ my_hidden_​​field”将ID添加到您的隐藏字段中以对其进行命名。

Then, trigger a javascript function when the user click on a star : 然后,当用户单击星号时触发javascript函数:

$("#second_star").click(function() {
  $("my_hidden_field").value = 2;
});

To show text inside div using javascript: 使用javascript在div中显示文本:

divObj = document.getElementById("rateit10");
            if ( divObj ){
                if ( divObj.textContent ){ //FF
                    alert ( divObj.textContent );
                }else{  //IE            
                    alert ( divObj.innerText );  //alert ( divObj.innerHTML );
                } 
            }  

To show innerdiv inside div using javascript: 要使用javascript在div中显示innerdiv,请执行以下操作:

myDivObj = document.getElementById("rateit10");
if ( myDivObj ) {
   alert ( myDivObj.innerHTML ); 
}else{
   alert ( "Text Found" );
}

To show text inside div using jquery 使用jQuery在div内显示文本

$(function(){
       var txt = $('#rateit10').text();
       alert(txt);
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM