有没有办法在rails admin config.model filterable选项中访问current_user。我尝试过使用:
bindings[:view]._current_user
但绑定对我来说是零
这是我一直在做的代码片段:
field :user do
filterable do #I want to access current user here
bindings[:view]._current_user #bindings is nil
end
pretty_value do
bindings[:view]._current_user #bindings is not nil
end
end
答案 0 :(得分:0)
我无法通过绑定方法访问它,所以我只是在我的应用程序控制器中创建了一个前置过滤器,它调用了一个方法,通过类方法在我的User模型中设置当前用户。
因此,在我的代码片段中,我可以像这样访问current_user:
public class LiveActivity extends AppCompatActivity {
String[] name = {"1","1","1","1"};
String[] nickname = {"MidnightSnack","MidnightSnack","MidnightSnack","MidnightSnack"};
String[] date = {"March 1,2016","March 1,2016","March 1,2016","March 1,2016"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.live_main);
Menu();
Bundle();
RecycleView();
init();
}
public void Bundle(){
Bundle getbundle =this.getIntent().getExtras();
ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
switch(getbundle.getString("theme")){
case "Recomended":
iRecomended.setVisibility(View.VISIBLE);
break;
case "Live":
ImageView iLive = (ImageView)findViewById(R.id.live_line);
iLive.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Like":
ImageView iLike = (ImageView)findViewById(R.id.like_line);
iLike.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Favorite":
ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
iFavorite.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "History":
ImageView iHistory = (ImageView)findViewById(R.id.history_line);
iHistory.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Feedback":
ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
iFeedback.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
case "Settings":
ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
iSettings.setVisibility(View.VISIBLE);
iRecomended.setVisibility(View.INVISIBLE);
break;
}
}
public void Menu(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//remove ToolBar Tittle
getSupportActionBar().setDisplayShowTitleEnabled(false);
final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
// setting menu width
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.left_fragment);
ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
tool_menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.showMenu();
}
});
}
public void RecycleView(){
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ArrayList<PostOfLiveActivity> data = new ArrayList<>();
for(int i = 0; i < name.length; i++) {
data.add(new PostOfLiveActivity(name[i], null, nickname[i],date[i]));
}
AdapterOfLiveActivity adapter = new AdapterOfLiveActivity(this, data);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public void init(){
LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
mRecomended.setOnClickListener(new OnClick());
LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
mLive.setOnClickListener(new OnClick());
LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
mLike.setOnClickListener(new OnClick());
LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
mFavorite.setOnClickListener(new OnClick());
LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
mHistory.setOnClickListener(new OnClick());
LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
mFeedback.setOnClickListener(new OnClick());
LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
mSettings.setOnClickListener(new OnClick());
}
private class OnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
switch (v.getId()) {
case R.id.recomended:
intent.setClass(LiveActivity.this,MainActivity.class);
bundle.putString("theme", "Recomended");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Recomended");
break;
case R.id.live:
intent.setClass(LiveActivity.this,LiveActivity.class);
bundle.putString("theme", "Live");
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(0, 0);
Log.e("theme","Live");
break;
default:
break;
}
}
}
}
答案来自: https://amitrmohanty.wordpress.com/2014/01/20/how-to-get-current_user-in-model-and-observer-rails/