在@处分割字符串(电子邮件)并设置为用户名

时间:2019-07-31 15:30:37

标签: ruby-on-rails

我对ruby on rails极为陌生(我相信是5个),我正尝试从为Devise注册系统输入的电子邮件中分离用户并将其设置为我的用户名。

即使从哪​​里开始,我也真的很茫然。 (所以我从这里开始。)我了解我想做什么,并找到了一些关于ruby的代码,但是我不确定如何跳到在Rails中使用ruby做到这一点。 (新!)

@username = params[:email].split(/@/)

在注册表中,我试图在输入电子邮件后立即创建用户名。我得到的错误是“ nil:NilClass的未定义方法'split'”。 (我确信还有更多后续活动。

1 个答案:

答案 0 :(得分:0)

首先,对nil:NilClass使用'undefined method'split'',因为已提交参数中的params [:email]为空,您应该说电子邮件是强制性的。其次,如果需要在@之前提取字符,则可以使用正则表达式进行引用

 private void showNotification() {

    createNotificationChanel();

    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.status_bar);
    RemoteViews bigViews = new RemoteViews(getPackageName(),
            R.layout.status_bar_expanded);

    // trying to receive intent
    Intent intent = new Intent();

    String title = intent.getStringExtra("title");


// showing default album image
    views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
    views.setViewVisibility(R.id.status_bar_album_art, View.GONE);
    bigViews.setImageViewBitmap(R.id.status_bar_album_art,
            Constants.getDefaultAlbumArt(this));

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Intent previousIntent = new Intent(this, NotificationService.class);
    previousIntent.setAction(Constants.ACTION.PREV_ACTION);
    PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
            previousIntent, 0);

    Intent playIntent = new Intent(this, NotificationService.class);
    playIntent.setAction(Constants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

    Intent nextIntent = new Intent(this, NotificationService.class);
    nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);

    Intent closeIntent = new Intent(this, NotificationService.class);
    closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
    PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
            closeIntent, 0);

    views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);

    views.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);

    views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);

    views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);

    views.setImageViewResource(R.id.status_bar_play,
            R.drawable.apollo_holo_dark_pause);
    bigViews.setImageViewResource(R.id.status_bar_play,
            R.drawable.apollo_holo_dark_pause);

    views.setTextViewText(R.id.status_bar_track_name, "Song Title");
    bigViews.setTextViewText(R.id.status_bar_track_name, "Song Title");

    views.setTextViewText(R.id.status_bar_artist_name, "Artist Name");
    bigViews.setTextViewText(R.id.status_bar_artist_name, "Artist Name");

    bigViews.setTextViewText(R.id.status_bar_album_name, "Album Name");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        status = new Notification.Builder(this,CHANEL_ID).build();
    }
    status.contentView = views;
    status.bigContentView = bigViews;
    status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.mipmap.ic_launcher_custom;
    status.contentIntent = pendingIntent;
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
}