rails-如何使用链接更改数据库值 [英] rails - how to change database values with a link

查看:43
本文介绍了rails-如何使用链接更改数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails创建一个网站,并且迫切需要我如何创建链接的帮助,该链接被单击时将更新数据库中的属性,但仅在单击时才会显示.我有以下代码:

I am making a website using rails and I am in desperate need of help of how do I create a link that when clicked on it will update an attribute in the database but only when clicked on.I have this code:

<%= link_to (myproperty_path) do %>
<% @player.update_attribute("energy", @player.energy + 2) %><span>Take a Nap</span>

<%end%>

但是问题是,每当我刷新页面时,它都会更新属性,而当我转到网站上的另一个页面时,它会再次更新属性.当我单击它时它可以工作,但是我希望它仅在单击时才能工作,仅此而已.另外,如果我在同一页面上有两个链接,则单击一个链接的行为就像我同时单击两个链接一样.这是我的myproperty页面的内容:

but the problem with this is that whenever I refresh the page it updates the attribute and when I go to another page on the website it updates the attribute again. It works when I click on it but I want it to work only when clicked on and that's it. Also, if I have two of these links on the same page, clicking on one link will act as if I'm clicking on both of the links at the same time. Here is what I have for the myproperty page:

<%= render 'sidebar' %>
<div id="yui-main" class="yui-b">
<h2>My Property</h2>
<br \>
<p><b>Property: </b><%= @player.property %><br \><br \> 
<%= link_to (myproperty_path) do %>
    <span>Take a Nap</span>
    <% if @player.energy <= 98 && @player.energy != 100 %>
    <% @player.update_attribute("energy", @player.energy + 2) %>
<% end %>
<% end %>
<br \>
<%= link_to (myproperty_path) do %>
    <span>Sleep</span>
    <% if @player.energy <= 96 && @player.energy != 100 %>
    <% @player.update_attribute("energy", @player.energy + 4) %>
<% end %>
<% end %>



<% if @player.property != "My Car" %>
    <b>Rent: </b><br \>
    <br \>
    <b>Bedroom</b><br \>
<% end %>

当我单击其中一个链接时,它会增加6而不是2或4.在此处输入代码该链接位于myproperty页面上,我希望它返回到单击myproperty页面.我在任何地方都找不到解决方案,如果有人可以帮助我解决问题,我将不胜感激.

When I click on one of the links it adds 6 to the player's energy instead of just 2 or 4. enter code hereThe link is on the myproperty page and I want it to go back to the myproperty page when clicked. I haven't found a solution anywhere, I would really appreciate it if someone could help me out with this.

推荐答案

您正在以一种根本上不正确的方式进行此操作.Rails并不是在您的视图中包含业务逻辑,尤其是用于更新记录的逻辑!

You are going about this in a fundamentally incorrect way. Rails is not designed to have business logic in your views - especially logic that updates records!

您想要的是类似的东西

<%= link_to "Take a nap", {:controller => "player_actions", :action => "nap", :id => @player.id} %>
<%= link_to "Sleep", {:controller => "player_actions", :action => "sleep", :id => @player.id } %>

在您的视图中,以及您的PlayerActionsController中的相应控制器动作,或者您想调用的任何东西

in your view, and a corresponding controller action in your PlayerActionsController, or whatever you want to call it

def nap
  player = Player.find(params[:id])
  player.update_attribute(:energy, player.energy + 2)
end

def sleep
  player = Player.find(params[:id])
  player.update_attribute(:energy, player.energy + 4)
end

这样,仅当用户单击链接时,操作才会发生.当然,您还需要处理任何重定向或Ajax渲染以及验证等.但这通常是应该构造Rails代码的方式.

This way the actions only happen when the user clicks the link. Of course you will need to handle any redirects or ajax rendering as well, as well as validation, etc. But this is generally how your Rails code should be structured.

这篇关于rails-如何使用链接更改数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆