将api路由添加到现有项目

时间:2016-10-07 05:02:58

标签: ruby-on-rails devise

我在当前项目中使用 Rails 4.2.1 。我正在使用设计进行身份验证。现在我想为移动设备添加api以及我现有的所有路由,因为我选择使用 devise_token_auth gem。我在User表中添加了一个列标记来存储令牌并添加到routes.rb

public class MyScaleGestures implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener, GestureDetector.OnGestureListener {
        private View view;
        private ScaleGestureDetector gestureScale;
        private float scaleFactor = 1;
        private boolean inScale = false;
        float dX, dY;

        public MyScaleGestures(Context c){ gestureScale = new ScaleGestureDetector(c, this); }

        @Override
        public boolean onTouch(View touchedview, MotionEvent event) {
            //scale the view
                this.view = touchedview;
                gestureScale.onTouchEvent(event);
            switch (event.getAction()){

                case MotionEvent.ACTION_DOWN:
                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                        view.animate().x(event.getRawX() + dX)
                                .y(event.getRawY() + dY)
                                .setDuration(0)
                                .start();
                        break;
                default:
                    return false;
            }
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scaleFactor *= detector.getScaleFactor();
            scaleFactor = (scaleFactor < 1 ? 1 : scaleFactor); // prevent our view from becoming too small //
            scaleFactor = ((float)((int)(scaleFactor * 100))) / 100; // Change precision to help with jitter when user just rests their fingers //
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            inScale = true;
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) { inScale = false; }


        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {
                    Log.e("clicked?", "I think so");
                    AlertDialog.Builder alert = new AlertDialog.Builder(context);
                    alert.setTitle(getResources().getString(R.string.delete_alert_title));
                    alert.setMessage(getResources().getString(R.string.delete_alert_message));
                    alert.setPositiveButton(getResources().getString(R.string.delete_confirm), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            picture.setVisibility(View.GONE);
                            btnaddImage.setClickable(false);
                            dialogInterface.dismiss();
                        }
                    });
                    alert.setNegativeButton(getResources().getString(R.string.delete_decline), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        //@Override
        public boolean onScroll(MotionEvent event1, MotionEvent event2, float x, float y) {
            float newX = view.getX();
            float newY = view.getY();
            if(!inScale){
                newX -= x;
                newY -= y;
            }
            WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
            Display d = wm.getDefaultDisplay();
            Point p = new Point();
            d.getSize(p);

            if (newX > (view.getWidth() * scaleFactor - p.x) / 2){
                newX = (view.getWidth() * scaleFactor - p.x) / 2;
            } else if (newX < -((view.getWidth() * scaleFactor - p.x) / 2)){
                newX = -((view.getWidth() * scaleFactor - p.x) / 2);
            }

            if (newY > (view.getHeight() * scaleFactor - p.y) / 2){
                newY = (view.getHeight() * scaleFactor - p.y) / 2;
            } else if (newY < -((view.getHeight() * scaleFactor - p.y) / 2)){
                newY = -((view.getHeight() * scaleFactor - p.y) / 2);
            }

            view.setX(newX);
            view.setY(newY);

            return true;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }
    }

在模型

中的 User.rb 中添加了此行
namespace :api do
   namespace :v1 do
     mount_devise_token_auth_for 'User', at: '/auth'
   end
end

我还在controllers文件夹中创建了devise_token_auth文件夹,并添加了这里列出的文件https://github.com/lynndylanhurley/devise_token_auth/tree/master/app/controllers/devise_token_auth

我这样做了吗?我可以在一个应用程序中使用两个身份验证吗?(对于api和web) 请帮忙。提前致谢

0 个答案:

没有答案