如何通过 active_scaffold 添加条件 link?
How to add conditional link via active_scaffold?
正如大家常说的,我是 Ruby Rails) 的新手,遇到以下问题:
我有一个搜索表单
https://www.dropbox.com/s/hmr85xh3ra28nsf/Screen%20Shot%202015-07-10%20at%2023.19.07.png?dl=0
我希望能够添加新的 msisdn(如果它不存在)。所以我做了假插入并想将条件 link 添加到我的 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
但返回错误:找不到方法。
如何实现我的条件 link?
我已经通过以下方式解决了这个问题:
我将参数添加到我的 link - :html_options = {}
config.action_links.add :submit_create, :label => "Create blacklisted
user", :type => :record, :confirm => "Are you sure?", :inline => true,
:method => :get, :html_options = {}
并在我的控制器中操作参数:
active_scaffold_config.action_links.each do |link|
link.html_options={:hidden=>true} if ['Create blacklisted user'].include? link.label
end
正如大家常说的,我是 Ruby Rails) 的新手,遇到以下问题:
我有一个搜索表单 https://www.dropbox.com/s/hmr85xh3ra28nsf/Screen%20Shot%202015-07-10%20at%2023.19.07.png?dl=0
我希望能够添加新的 msisdn(如果它不存在)。所以我做了假插入并想将条件 link 添加到我的 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
但返回错误:找不到方法。 如何实现我的条件 link?
我已经通过以下方式解决了这个问题:
我将参数添加到我的 link - :html_options = {}
config.action_links.add :submit_create, :label => "Create blacklisted user", :type => :record, :confirm => "Are you sure?", :inline => true, :method => :get, :html_options = {}
并在我的控制器中操作参数:
active_scaffold_config.action_links.each do |link|
link.html_options={:hidden=>true} if ['Create blacklisted user'].include? link.label
end