暂时隐藏前端点击记录 - rails 4.2

时间:2016-08-01 02:07:33

标签: ruby-on-rails

实际上,我想在点击隐藏链接时隐藏相应的记录。

请参阅屏幕截图以便更好地理解,如下所示;

screenshot.png

在点击链接之前,隐藏属性为false,如下面的终端所示;

Screenshot1.png

点击链接后,它会抓取相应的ID并调用控制器上的隐藏方法,如下所示;

Screenshot2.png

问题是它既没有隐藏相应的行,也没有从余额计算中转义这些值,而是被重定向到xvaziris_url。

我一次又一次地尝试以解决这个问题,但遗憾的是无法获得理想的结果。

xvaziris_controller.rb

def index
        @xvaziris = Xvaziri.find_by hidden: false
        @xvaziris = Xvaziri.search(params[:search])

        respond_to do |format|
            format.js
            format.html 
        end 
    end

def hide
        @xvaziri = Xvaziri.find(params[:id])
        @xvaziri.hidden = true
        flash[:notice] = 'Xvaziri was successfully hidden.'
        redirect_to xvaziris_url    
    end
end

的routes.rb

resources :xvaziris do
        member do
            get :hide
        end
    end

_xvaziri.html.erb

<td class="col-1"><%= link_to "Hide", controller: "xvaziris", action: "hide", id: xvaziri, method: :get %></td>

欢迎任何建议。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您不保存操作的结果。此代码@xvaziri.hidden = true只是将hidden属性设置为true。您需要保存@xvaziri

试试这个解决方案。

def hide
  @xvaziri = Xvaziri.find(params[:id])
  @xvaziri.update_attribute(:hidden, true)
  flash[:notice] = 'Xvaziri was successfully hidden.'
  redirect_to xvaziris_url    
end

<强>更新

我刚刚复制了你的应用程序并找出了问题。

首先,find_by仅返回第一个结果为describeet http://localhost:8000

第二,你总是得到xvaziris实例,你的搜索方法找不到find_by

所以,让我们来看看你控制器的索引动作

def index
    @xvaziris = Xvaziri.find_by hidden: false
    # find first not hidden xvaziri and
    # store in into @xvaziris variable

    @xvaziris = Xvaziri.search(params[:search])
    # find xvaziris according to search performed
    # and store it into @xvaziris variable 
    # so, at this place you always have xvaziris 
    #that has been found but they may be hidden.

    respond_to do |format|
        format.js
        format.html 
    end 
end

以下是一些建议

首先在Xvaziri model

中实现一些方法
class Xvaziri < ActiveRecord::Base

  scope :visible, -> { where(hidden: false) }
  # returns only visible xvaziri's

  def hide
    update_attribute(:hidden, true)
  end

  def show
    update_attribute(:hidden, false)
  end
end

然后在你的控制器中:

def index
  @xvaziris = Xvaziri.search(params[:search]).visible

  respond_to do |format|
    format.js
    format.html 
  end 
end

def hide
  @xvaziri = Xvaziri.find(params[:id])
  @xvaziri.hide
  flash[:notice] = 'Xvaziri was successfully hidden.'
  redirect_to xvaziris_url    
end

显示隐藏的xvaziri

# app/models/xvaziri.rb
scope :hidden, -> { where(hidden: true) }

# app/controllers/xvaziris_controller.rb
def hidden
  @xvaziris = Xvaziri.search(params[:search]).hidden
end

def unhide
  xvaziri = Xvaziri.find(params[:id])
  xvaziri.show
  flash[:notice] = "Xvaziri is now visible."
  redirect_to hidden_url
end

#app/views/xvaziris/hidden.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing Xvaziris</h1>

<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Description</th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% if @xvaziris.any? %>
      <% @xvaziris.each do |xvaziri| %>
        <tr>
          <td><%= xvaziri.id %></td>
          <td><%= xvaziri.description %></td>
          <td><%= link_to 'unhide', unhide_xvaziri_path(xvaziri), method: :put %></td>
        </tr>
      <% end %>
    <% end %>
  </tbody>
</table>

# config/routes.rb
resources :xvaziris do 
  member do 
    get :hide
    get :unhide 
  end
end
get "hidden" => "xvaziris#hidden"

但使用:get方法修改模型并不好。请考虑使用:put方法。

在我看来,示例链接使用:put方法