简体   繁体   English

Rails:如何从视图切换布尔字段?

[英]Rails: How to toggle a boolean field from a view?

I have a boolean field called "saved" in my database. 我的数据库中有一个布尔字段,称为“保存”。 I want to toggle this field by clicking on a text link that changes from "Save" to "Unsave" depending on the situation, and updates my "Customer" table with 0 or 1. I imagine Javascript may be a way to go for this but I am not experienced enough (yet!) in Javascript to know how to code it. 我想通过单击一个文本链接来切换此字段,该文本链接根据情况从“保存”更改为“取消”,并用0或1更新我的“客户”表。我想Java语言可能是解决此问题的一种方法但是我对Javascript的经验还不够(到目前为止!),不知道如何进行编码。

I've rolled back the question to keep it shorter. 我回滚了问题以使其更短。 Here is my exact code. 这是我的确切代码。

#employers controller
def save_toggle
  @matching = Matching.find(params[:id])
  if @matching.employer_stars == false
    @matching.employer_rejects = false # If saving a match, remove any existing rejection.
  end
  @matching.employer_stars = !@matching.employer_stars
  @matching.save
  render :partial => "save_unsave_buttons", :layout => false
end

#view home.html.erb
<%= render :partial => "save_unsave_buttons", :locals => {:matching => matching} %>

#partial _save_unsave_buttons.html.erb
<div id="save_buttons" class="buttonText"> #latter is just for CSS layout
  <% if @matching.employer_stars %>
    <%= link_to_remote "Unsave",
      :url => {:action => "save_toggle", :id => matching.id},
      :update => {:success => "save_buttons", :failure => "Error"} %>
  <% else %>
    <%= link_to_remote "Save",
      :url => {:action => "save_toggle", :id => matching.id},
      :update => {:success => "save_buttons", :failure => "Error"} %>
  <% end %>
</div>

The database is working but the toggle text isn't switching. 数据库正在工作,但切换文本未切换。 To @nathanvda: I'm really sorry for being such a pain - I want to confirm your answer but I know if I do I'll just leave this for a while then come back to it and get frustrated again! 致@nathanvda:非常抱歉让您感到如此痛苦-我想确认您的回答,但我知道如果我这样做,我将把它搁置一会儿然后再回到它并再次感到沮丧! Thanks man. 谢啦。

You have to define a controller-method, which sets your saved attribute. 您必须定义一个控制器方法,该方法可以设置您saved属性。 In your view you can then link to this method using link_to_remote . 在您看来,您可以使用link_to_remote链接到此方法。

That should get you started. 那应该让您开始。

--Update: after updated question: -更新:更新问题后:

You should create a partial that renders your save/unsave button like this, call it "_save_unsave_buttons.html.erb" : 您应该创建一个部分来渲染您的保存/取消保存按钮,将其命名为“ _save_unsave_buttons.html.erb”:

<div id="save_buttons">
  <% if matching.employer_stars %>
    <%= link_to_remote "Unsave",
       :url => {:action => "save_toggle", :id => matching.id},
       :update => {:success => "save_buttons", :failure => "Error"} %>
  <% else %>
    <%= link_to_remote "Save",
       :url => {:action => "save_toggle", :id => matching.id},
       :update => {:success => "save_buttons", :failure => "Error"} %>
  <% end %>
</div>

This partial will render you correct save-buttons, and upon update the containing div is updated/replaced by the result of your controller action. 此部分将为您提供正确的保存按钮,并且在更新时,包含的div将通过控制器操作的结果进行更新/替换。

From inside your main view, write 在您的主视图中,编写

<%= render :partial => "save_unsave_buttons", :locals => {:matching => match } %>

where you want the buttons to be visible. 您希望按钮可见的位置。

And inside your controller: 在您的控制器内部:

def save_toggle
  @matching = Matching.find(params[:id])
  @matching.employer_stars = !@matching.employer_stars
  @matching.save
  render :partial => "save_unsave_buttons", :locals => {:matching => @matching}, :layout => false
end

Good luck! 祝好运!

--Update again: so i presume you render a set of @matchings, i would change the naming between the collection and the item a bit, to prevent more confusion and accidental mistypings. -再次更新:所以我假设您呈现了一组@matchings,我会稍微更改集合和项目之间的命名,以防止出现更多的混乱和意外的迷惑。

But actually this is pretty easy: 但这实际上很简单:

@matchings.each do |match|
  .. build your view here ..
  <%= render :partial => "save_unsave_buttons", :locals => {:matching => match}
end

and in your partial you can then use the correct matching everywhere. 然后您就可以在局部使用正确的matching

Just a notice: 只是一个通知:

Your save_toggle method is not RESTful. 您的save_toggle方法不是RESTful的。 The HTTP PUT verb should be implemented idempotent (See oa http://en.wikipedia.org/wiki/Idempotence#Examples ), which means it should always do the same thing no matter how often you execute it. HTTP PUT动词应实现幂等(请参阅http://en.wikipedia.org/wiki/Idempotence#Examples ),这意味着无论执行频率如何,它都应始终执行相同的操作。 In your example, executing the save_toggle method once does not give the same result as executing it twice. 在您的示例中,执行一次save_toggle方法不会产生与两次执行相同的结果。

A better practice would be to make two methods, eg: 更好的做法是制作两种方法,例如:

def set_employer_stars
end

def unset_employer_stars
end

or whatever you want to call them. 或任何您想给他们打电话的东西。 Then you can also use these two different methods in the link_to_remote methods (because you now use save_toggle in both "Unsave" and "Save"). 然后,您还可以在link_to_remote方法中使用这两种不同的方法(因为现在在“取消保存”和“保存”中都使用save_toggle )。

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

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