在我的应用程序中,使用电报机器人向我的Rails api发送消息来创建用户。
创建用户后,它可以添加与之关联的Instagram帐户。 当用户添加新的Instagram帐户时,has_secure_token会生成随机令牌,并向用户发送此可过期链接:www.example.com/set/:token。
路线:
resources :instagram_users, except: [:new, :create, :edit]
get 'set/:token', to: 'instagram_users#edit', as: 'set'
控制器:
def edit
@instagram_user = InstagramUser.find_by(password_reset_token: params[:token])
end
def update
@instagram_user = InstagramUser.find(params[:id])
correct_user(@instagram_user, params[:token])
if @instagram_user.update_attributes(instagram_user_params)
flash[:success] = "Instagram user success!"
redirect_to dashboard_path
else
flash[:alert] = "Something went wrong!"
render 'edit'
end
end
private
def instagram_user_params
params.require(:instagram_user).permit(:password, :password_confirmation, :backup_code, :token)
end
def correct_user(ig_user, token)
redirect_to root_url unless ig_user.password_reset_token == token
end
我的观点:
edit.html.haml ('set/:token')
= form_for @instagram_user, url: instagram_user_path(@instagram_user.id), html: { class: 'form-horizontal' } do |f|
= render 'shared/error_messages'
= f.label :password
= f.password_field :password, class: 'form-control'
= f.label :password_confirmation, "Confirmation"
= f.password_field :password_confirmation, class: 'form-control'
= f.label :backup_code
= f.text_field :backup_code, class: 'form-control'
= hidden_field_tag :token, @instagram_user.password_reset_token
= f.submit "Save Changes", class: "btn btn-primary"
现在我已经尝试了几种不同的form_for方法,但它没有达到更新操作。
编辑:
rails routes:
root GET / pages#home
instagram_users GET /instagram_users(.:format) instagram_users#index
instagram_user PATCH /instagram_users/:id(.:format) instagram_users#update
PUT /instagram_users/:id(.:format) instagram_users#update
DELETE /instagram_users/:id(.:format) instagram_users#destroy
set GET /set/:token(.:format) instagram_users#edit
GET /:token/dashboard(.:format) instagram_users#show
提交表格时:
Started GET "/set/y2mSCzfbHZ1HgUxtbQnaMZsR?utf8=%E2%9C%93&_method=patch&authenticity_token=E4mLcqieWsibgCt1VWM%2FKDv1YS%2FWtpS8%2BYk82%2BmqP08
phCpvq62lebpUGTfHFSKD%2BDtO%2FtxX%2FKUHr4mP3iXYcw%3D%3D&instagram_user%5Bpassword%5D=[FILTERED]&instagram_user%5Bpassword_confirmation%5D=[FIL
TERED]&instagram_user%5Bbackup_code%5D=1234&token=y2mSCzfbHZ1HgUxtbQnaMZsR&commit=Save+Changes" for ::1 at 2017-11-18 09:00:31 -0600
Processing by InstagramUsersController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"E4mLcqieWsibgCt1VWM/KDv1YS/WtpS8+Yk82+mqP08phCpvq62lebpUGTfHFSKD+DtO/txX/KUHr4mP3iXYcw==",
"instagram_user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "backup_code"=>"1234"}, "token"=>"y2mSCzfbHZ1HgUxtbQnaMZsR
", "commit"=>"Save Changes"}
InstagramUser Load (31.2ms) SELECT "instagram_users".* FROM "instagram_users" WHERE "instagram_users"."password_reset_token" = $1 LIMIT $2
[["password_reset_token", "y2mSCzfbHZ1HgUxtbQnaMZsR"], ["LIMIT", 1]]
Rendering instagram_users/edit.html.haml within layouts/application
Instagram Load (0.6ms) SELECT "instagrams".* FROM "instagrams" WHERE "instagrams"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered shared/_error_messages.html.haml (9.9ms)
Rendered instagram_users/edit.html.haml within layouts/application (72.6ms)
Completed 200 OK in 263ms (Views: 203.4ms | ActiveRecord: 31.8ms)