Rails / Devise / Omniauth组合说电子邮件是零,但事实并非如此

时间:2017-01-07 15:18:11

标签: ruby-on-rails ruby devise omniauth

我正在设置Devise&按照我常常做的轨道广播的Omniauth,现在我只使用Rails 5.0.1和ruby 2.3.3

当我去添加用户时,甚至没有使用Omniauth,只需使用基本的电子邮件+密码+确认页面重新加载,错误为“电子邮件不能为空”。我可以看到参数包括电子邮件。

我用Pry来调查 - 事情在这里变得奇怪。

public class Notification extends Service {
    String datanews;
    String titlenotif;
    String destnotif;
    MyAsynk asynk;
    final String SAVED_TEXT = "saved_text";
    String checker;
    SharedPreferences sPref;

    @Override
    public void onCreate() {
        super.onCreate();
        Timer timer = new Timer();
        timer.schedule(new UpdateTimeTask(), 0, 1800000); //тикаем каждые 30 мinute без задержки 1800000
    }

    class UpdateTimeTask extends TimerTask {
        public void run() {
            sPref = getSharedPreferences("MyPref",MODE_PRIVATE);
            String savedText = sPref.getString(SAVED_TEXT, "");
            checker = sPref.getString(savedText, "0");

            if(datanews != checker){
                asynk = new MyAsynk();
                asynk.execute();
                createNotification(getApplicationContext());//пушим уведомление
            } else {
                asynk = new MyAsynk();
                asynk.execute();
            }
        }
    }

    class MyAsynk extends AsyncTask<Void,Void,StringBuilder> {
        @Override
        protected StringBuilder doInBackground(Void... voids) {
            StringBuilder stringBuilder = new StringBuilder();
            String key = "0aa2713d5a1a4aad9a914c9294f6a22b";
            try {
                URL url = new URL("https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=" + key);
                URLConnection uc = url.openConnection();
                uc.connect();
                BufferedInputStream in = new BufferedInputStream(uc.getInputStream());
                int ch;
                while ((ch = in.read()) != -1) {
                    stringBuilder.append((char) ch);
                }
            } catch (Exception e) {e.printStackTrace();}

            return stringBuilder;
        }

        @Override
        protected void onPostExecute(StringBuilder stringBuilder) {
            try {
                JSONObject jsonObject = new JSONObject(stringBuilder.toString());
                JSONArray array = jsonObject.getJSONArray("articles");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    String title = object.getString("title");
                    String desc = object.getString("description");
                    String newsdata = object.getString("publishedAt");
                    datanews = newsdata;
                    titlenotif = title;
                    destnotif = desc;
                }
            }
            catch (Exception e){e.printStackTrace();}
        }
    }

    private void createNotification(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder ncBuilder = new NotificationCompat.Builder(context);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        ncBuilder.setVibrate(new long[]{500});
        ncBuilder.setLights(Color.WHITE, 3000, 3000);
        ncBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        ncBuilder.setContentIntent(pIntent);
        ncBuilder.setContentTitle(titlenotif + "");
        ncBuilder.setContentText(destnotif + "");
        ncBuilder.setTicker("You have news!");
        ncBuilder.setSmallIcon(R.drawable.news_icon);
        ncBuilder.setAutoCancel(true);
        manager.notify((int)System.currentTimeMillis(),ncBuilder.build());
    }
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

您可以看到,如果我访问@user,它将返回包含该电子邮件的哈希。但是,如果我尝试@ user.email,我会得到零。当我要求@ user.errors时,消息显示电子邮件不能为空。

我一直在这个上打猎。在此先感谢您的帮助!

-

[1] pry(#<RegistrationsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"[removed this!]", "user"=><ActionController::Parameters {"email"=>"test@test.com", "password"=>"testtest", "password_confirmation"=>"testtest"} permitted: false>, "commit"=>"Sign up", "controller"=>"registrations", "action"=>"create"} permitted: false>
[3] pry(#<RegistrationsController>)> @user
=> #<User id: nil, email: "test@test.com", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>
[4] pry(#<RegistrationsController>)> @user.class
=> User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, first_name: string, last_name: string, image_url: string, headline: string, dob: date, gender: string, webpage_url: string, twitter: string, linkedin: string, blog_url: string, description: text, country: string, state: string, city: string, phone: string, full_bio: text, type: string, profile_claimed: boolean)
[5] pry(#<RegistrationsController>)> @user.email
=> nil
[7] pry(#<RegistrationsController>)> @user.errors
=> #<ActiveModel::Errors:0x007f9de2c82da0
 @base=
  #<User id: nil, email: "test@test.com", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, image_url: nil, headline: nil, dob: nil, gender: nil, webpage_url: nil, twitter: nil, linkedin: nil, blog_url: nil, description: nil, country: nil, state: nil, city: nil, phone: nil, full_bio: nil, profile_claimed: nil>,
 @details={:email=>[{:error=>:blank}, {:error=>:blank}]},
 @messages={:email=>["can't be blank"], :password=>[], :password_confirmation=>[]}>
[8] pry(#<RegistrationsController>)> @user.email.class
=> NilClass

Puma服务器。

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:email])
  end
end

- rails console

Started POST "/users" for ::1 at 2017-01-07 20:42:57 -0500
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tfBeZtXEFdZV9Rx3dJgCQJ+9hHuvgFcbc3dD9D7Q6yKki4GmlVw17/qlDm2squ3hKngX3Juu12iaM1Dki9qXig==", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendering registrations/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (0.7ms)
  Rendered registrations/new.html.erb within layouts/application (4.6ms)

1 个答案:

答案 0 :(得分:0)

我相信permitted: false表示您的参数不被允许且无法保存。您是否为Users控制器设置了强大的参数?