我在rails app中安装了cancan gem
我已经阻止了我的能力。这样的人......
can [:action1,:action2,:action3] ,Obj do |x|
# I wante to get current action, like this
if action == action1
#do something
end
end
我不想将特殊区块中的每个动作分开
如何在能力阻止中获得当前行动?
答案 0 :(得分:1)
您可以使用操作作为变量初始化Ability类实例方法:
#models/ability.rb
class Ability
include CanCan::Ability
def initialize(user, action)
can action.to_sym, Obj do |x|
#do something with your action (and user?)
end
end
end
现在您可以初始化新功能,将当前操作传递给它:
#application_controller.rb
def current_ability
@current_ability ||= Ability.new(current_user, params[:action])
end
并使用存储在默认参数:action
答案 1 :(得分:0)
正是我需要做的! 我最终得到了这个解决方案:
<Formik
initialValues={{}}
validationSchema={() => Yup.object().shape({`enter your validation keys here`})}
onSubmit={(values) => {
console.log('values after submit', values);
}}
>
{(formikProps) => {
const { values, handleChange } = formikProps;
return (
<Form>
<label>
name
<input name="name" onChange={handleChange} />
</label>
<label>
age
<input name="age" type="number" onChange={handleChange} />
</label>
<button
type="button"
onClick={() => {
console.log('show filled fields', values);
}}
>
Show Filled Fields
</button>
<button type={'submit'}> Submit</button>
</Form>
);
}}
</Formik>