2015-10-26 37 views
0

我有一個簡單的博客設置與資源路由,它允許他們查看博客之前驗證用戶。我可以通過「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' 
+1

您可以在提交表單時發佈在服務器日誌中生成的'params'問題嗎? – Pavan

+0

我已更新服務器日誌 – Ctpelnar1988

+0

爲什麼你要通過blogsController更新/創建用戶? – illusionist

回答

0

改成這樣:

private 
def message_params 
    params.require(:user).permit(:email, :password, :password_confirmation) 
end 
+0

我試過這個expamle,但是沒有解決問題。 – Ctpelnar1988

0

的問題是在控制器:

before_action :confirm_logged_in, :except => [:login, :attempt_login, :new, :logout] 

我失蹤 「:打造」,這解決了問題:

要進行3210
before_action :confirm_logged_in, :except => [:login, :attempt_login, :new, :logout, :create] 
1

地段修復 - 這裏是我建議:

#config/routes.rb 
root "posts#index" 
resources :posts, only: [:index, :show] 

resources :users, only: [:new, :create], path: "", path_names: { new: "signup", create: "signup" } 
resources :sessions, only: [:new, :create, :destroy], path: "", path_names: { new: "login", create: "login", destroy: "logout" } 

這是你的控制器分割成不同的行動。

這可能看起來很複雜,但請相信我,從長遠來看,它對您的應用來說會更好。


#app/controllers/application_controller.rb 
class ApplicationController < ActionController::Base 
    #put all your application-centric logic here 
    before_action :confirm_logged_in 

    private 

    def confirm_logged_in 
     #don't need to "return" anything here 
     redirect_to new_sessions_path, notice: "Please login" unless session[:user_id] 
    end 
end 

而不是 「博客」,稱之爲 「上崗」。你的應用程序邏輯停留在應用控制器:

#app/controllers/posts_controller.rb 
class PostsController < ApplicationController 
    def index 
     @posts = Post.all 
    end 

    def show 
     @post = Post.find params[:id] 
    end 
end 

這是處理用戶會話(登錄)(偷來的form Devise):

#app/controllers/sessions_controller.rb 
class SessionsController < ApplicationController 
    skip_before_action :confirm_logged_in 

    def new 
    end 

    def create 
     if params[:email].present? && params[:password].present? 
      if User.exists? email: params[:email] 
       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 root_path, notice: "Successful Login" 
     else 
      redirect_to :new, notice: "Sorry, there was an error" 
     end 
    end 

    def destroy 
     session.delete(:user_id) 
     session.delete(:email) 
     redirect_to new_sessions_path, notice: "Logged Out" 
    end 

end 

我能夠 「註冊」通過輸入數據通過「軌道C」,但當試圖提交表格它不保存到分區

這是它應該做的事:

#app/controllers/users_controller.rb 
class UsersController < ApplicationController 
    def new 
     @user = User.new 
    end 

    def create 
     @user = User.new user_params 
     @user.save 
    end 

    private 

    def user_params 
     params.require(:user).permit(:email, :password, :other, :params) 
    end 
end 

然後您就可以使用以下幾種觀點:

#app/views/users/new.html.erb 
<%= form_for @user do |f| %> 
    <%= f.email_field :email %> 
    <%= f.password_field :password %> 
    <%= f.password_field :password_confirmation %> 
    <%= f.submit %> 
<% end %> 

- 除了

作爲,你會從受益大汗使用Devise gem

這將基本上從您的應用程序中刪除所有session功能,包括它在Devise。這工作真的很好,我會高度推薦它。

+1

謝謝豐富。我最終創建了另一個名爲users_controller.rb的控制器,並將所有的邏輯移到那裏以避免衝突。一切正在順利進行。我之前使用過Devise,但是想手工製作註冊/登錄部分以瞭解更多背後的邏輯。 – Ctpelnar1988

+1

不錯,很高興聽到某些有競爭力的人自己放手,而不是期待別人爲他們做這一切:D –

相關問題