简体   繁体   English

如何使用jQuery更新Rails模型上的字段?

[英]How can I use jQuery to update a field on my rails model?

I have a model, Player, that has a field total_points. 我有一个播放器模型,其中有一个字段total_points。

On my home page, I have the players name and total_points: 在我的主页上,我有玩家的姓名和total_points:

<div id="player1">
  <h3><%= "#{@player1.first_name} #{@player1.last_name} (#{@player1.total_points})" %></h3>
</div>

I would like to have the total_points field on @player1 increase by 1 every time the player's name is clicked by the user. 我希望每次用户单击播放器名称时,@ player1上的total_points字段都增加1。 My jQuery (coffeescript) so far is: 到目前为止,我的jQuery(咖啡脚本)是:

/home.js.coffee
jQuery ->
  pointForPlayer1 = (e) ->
    e.preventDefault()
    console.log('player 1 clicked')

  $('#player1').click pointForPlayer1

This works (as in, it logs the message to the console), but I'm not sure how to have the click increase @player1.total_points by 1 and then re-render the message above (preferably without reloading the whole page). 这可以正常工作(例如,它将消息记录到控制台),但是我不确定如何将点击增加@ player1.total_points到1,然后重新渲染上面的消息(最好不重新加载整个页面)。 Thanks! 谢谢!

You'll have a better time if you adjust your HTML so that you can find the total points easily. 如果您调整HTML,这样会更美好,这样您就可以轻松找到总分。 Something like this: 像这样:

<div id="player1">
  <h3>
    <%= "#{@player1.first_name} #{@player1.last_name}" %>
    (<span class="total"><%= @player1.total_points %></span>)
  </h3>
</div>

Then you'd probably want to controller server side to increment the player's total points and send back the new total. 然后,您可能希望控制器服务器端增加玩家的总积分并发回新的总积分。 That would leave you with something like this: 那会给你留下这样的东西:

$ ->
  $('#player1').click (e) ->
    e.preventDefault()
    $.post 'whatever-the-url-is', (new_total) ->
      $('#player1 .total_points').html(new_total)

If you're not storing anything on the server then you could do it like this: 如果您没有在服务器上存储任何内容,则可以这样做:

$ ->
  $('#player1').click (e) ->
    e.preventDefault()
    $t = $('#player1 .total_points')
    $t.html(parseInt($t.html(), 10) + 1)

Since you have opted to use jquery (which I think may be the greatest invention of the computing age :) ), this is relatively easy. 由于您选择使用jquery(我认为这可能是计算机时代最伟大的发明:)),所以这相对容易。

You'll want to use the post command, which will post back to your app using AJAX. 您将需要使用post命令,该命令将使用AJAX发布回您的应用程序。 So, in your case the coffeescript would be $('#player1').click -> $.post(route, data, success method, datatype) per the JQuery docs 因此,在您的情况下, 根据JQuery文档 ,咖啡脚本将为$('#player1')。click-> $ .post(路由,数据,成功方法,数据类型)

The success method can then take the return data and deal with it. 然后,成功方法可以获取返回数据并进行处理。 If your html changes at this point, you can return html and replace the appropriate chunk of html with JQuery. 如果此时html发生更改,则可以返回html并将相应的html块替换为JQuery。 You can also return JSON, XML, etc. as you wish. 您还可以根据需要返回JSON,XML等。 I think in this case it sounds like returning a chunk of html may be appropriate. 我认为在这种情况下,听起来好像返回一块html可能是合适的。 You'll need an appropriate view to render to do it. 您需要一个适当的视图来进行渲染。

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

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