我有一個簡單的博客設置與資源路由,它允許他們查看博客之前驗證用戶。我可以通過「rails c」輸入數據來「註冊」,但是當試圖提交一個表單時,它不會保存到db,「create」方法返回true,就好像它已經保存了一樣。Rails:表單不保存到數據庫,但創建方法提交時返回true
控制器:blogs_controller.rb
class BlogsController < ApplicationController
before_action :confirm_logged_in, :except => [:login, :attempt_login, :new, :logout]
def new
@user = User.new
end
def create
@user = User.new(message_params)
if @user.save!
redirect_to(:action => 'login')
else
render 'new'
end
end
def login
end
def attempt_login
if params[:email].present? && params[:password].present?
found_user = User.where(:email => params[:email]).first
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
if authorized_user
session[:user_id] = authorized_user.id
session[:email] = authorized_user.email
redirect_to(:action => 'index')
else
redirect_to(:action => 'login')
end
end
def index
end
def show_month
@month_year = params[:month_year]
@year = params[:year]
end
def show_category
@category = params[:category]
end
def show_page
@date = params[:date]
end
def logout
session[:user_id] = nil
session[:email] = nil
flash[:notice] = "Logged out."
redirect_to(:action => "login")
end
private
def message_params
return params.require(:user).permit(:email, :password)
end
def confirm_logged_in
unless session[:user_id]
flash[:notice] = "Please login"
redirect_to(:action => 'login')
return false
else
return true
end
end
end
註冊:new.html.erb
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<%= form_for @user, url: blogs_path(@user) do |f| %>
<%= f.label :email %>
<%= f.text_field :email, :required => true %>
<br>
<%= f.label :password %>
<%= f.password_field :password, :required => true %>
<br>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :required => true %>
<br>
<%= f.submit("Submit")%>
<% end %>
</div>
</div>
</div>
型號:user.rb
class User < ActiveRecord::Base
has_secure_password
end
模式:schema.rb
ActiveRecord::Schema.define(version: 20151026022639) do
enable_extension "plpgsql"
create_table "messages", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "phone_number"
t.string "user_message"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
服務器日誌:PARAMS
Started POST "/attempt_login" for ::1 at 2015-10-26 02:57:57 -0400
Processing by BlogsController#attempt_login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"z4NgU41NLizqUE5vgXc44Q5n6VG36ho+5TVcPB4ixB7grZnCuwJY64+EWFzFIIJShbHEBNk3JutBuO4oalV2gQ==", "email"=>"[email protected]", "password"=>"[FILTERED]", "commit"=>"Login"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["email", "[email protected]"]]
Redirected to http://localhost:3000/blog_login
Completed 302 Found in 5ms (ActiveRecord: 0.3ms)
路線的routes.rb
get '/blog_signup' => 'blogs#new'
post 'blog_signup' => 'blogs#create'
get '/blog_login' => 'blogs#login'
post 'attempt_login' => 'blogs#attempt_login'
get '/blogs_home' => 'blogs#index'
get '/blog_month_archive' => 'blogs#show_month'
get '/blog_category_archive' => 'blogs#show_category'
get '/blog_page' => 'blogs#show_page'
get '/logout' => 'blogs#logout'
您可以在提交表單時發佈在服務器日誌中生成的'params'問題嗎? – Pavan
我已更新服務器日誌 – Ctpelnar1988
爲什麼你要通過blogsController更新/創建用戶? – illusionist