如何通过active_scaffold添加条件链接?

时间:2015-07-10 21:02:38

标签: ruby-on-rails ruby activescaffold

正如大家常说的那样,我是Ruby on Rails的新手,并且有以下问题:

我有一个搜索表单 https://www.dropbox.com/s/hmr85xh3ra28nsf/Screen%20Shot%202015-07-10%20at%2023.19.07.png?dl=0

我希望能够添加新的msisdn,如果它不存在的话。所以我做了假插入并希望添加条件链接到我的active_scaffold网格而不是当前存在的网格:

config.action_links.add :submit_create, :label => "Create blacklisted user", :type => :record, :confirm => "Are you sure?", :inline => true, :method => :get

这是我的控制器

>class CCare::UserstatusController < ApplicationController

>before_filter :filter_search

>  verify :method => :post,
>         :only => :stop,
>         :redirect_to => { :action => :index }
>         

    >  active_scaffold :users do |config|    
    >    config.label = "User Status"   
    >    config.list.columns = [:msisdn, :user_agent, :network ]    
    >    config.actions = [:search, :field_search, :list, :nested]   
    >    config.actions.swap :search, :field_search  
    >    config.field_search.columns = [:msisdn]   
    >    config.columns[:id].label = "ID"   
    >    config.columns[:id].search_sql = "users.id = ?"   
    >    config.columns[:id].search_ui = :text   
    >
    >    config.columns[:msisdn].search_sql = "users.msisdn = ?"   
    >    config.columns[:msisdn].search_ui = :text   
    >
    >    config.columns[:network].sort = {:sql => "network.network" }
    >    config.columns[:network].search_sql = "network.network"   
    >    config.columns[:network].search_ui = :select
    >    config.columns[:network].actions_for_association_links = [:show]   
    >    config.columns[:user_agent].label = "Device"           
    >    config.nested.add_link("Subscriptions", [:user_service_list])   
    >    config.nested.add_link("Contact History", [:user_contact])   
    >      config.action_links.add "show_stop_all", :label => "Stop All", :type > => :record, :crud_type => :create, :inline => true, :position => :after
    >    config.list.always_show_search = true
    >  end  

    >  def update_table  
    >    if params[:search].nil? and params[:nested].nil?  
    >      render :partial => "shared/empty_list", :layout => false  
    >    else  
    >      if not params[:search][:id].nil? and not params[:search][:id].empty? >and not params[:search][:id] =~ /^[0-9]+$/  
    >        flash[:error] = "ID is not a number"  
    >        render :partial => "shared/empty_list", :layout => false  
    >        return  
    >      end  
    >      if (is_msisdn_exist)  
    >        super  
    >      else  
    >        user = User.new  
    >        user.msisdn = params[:search][:msisdn]  
    >        user.user_list_id = 1  
    >        user.save  
    >        super  
    >      end  
    >    end  
    >  end  

我试图将方法is_msisdn_exist放入

active_scaffold :users do |config|  
  ....
end

但它返回错误:找不到方法。 我如何实现我的条件链接?

1 个答案:

答案 0 :(得分:0)

我已通过以下方式解决了这个问题:

我将参数添加到我的链接 - :html_options = {}

  

config.action_links.add:submit_create,:label =&gt; &#34;创建黑名单   user&#34;,:type =&gt; :record,:confirm =&gt; &#34;你确定吗?&#34;,:inline =&gt;真正,   :method =&gt; :get,:html_options = {}

并操纵控制器中的参数:

  

active_scaffold_config.action_links.each do | link |
    link.html_options = {:hidden =&gt; true}如果[&#39;创建列入黑名单的用户&#39;]。include? link.label
      端