0
我的登錄和註冊表單都在同一頁面上。我試圖根據填寫哪個表單來指定應該採用哪個'post'路線。註冊表單的帖子應發送到create_user操作,登錄表單的帖子應發送到創建操作。不管我在視圖中指定什麼,出於某種原因,create_user操作 - 即使是登錄表單。Rails指定路線
路線:
get '/login' => 'sessions#new', as: :users
post '/login' => 'sessions#create_user'
post '/login' => 'sessions#create'
查看:
<div class="forms">
<div class="login">
<h3>Login</h3>
<%= form_for(:session, url: {action: "create"}) do |x| %>
<%= x.email_field :email, :placeholder => " Email", :style => "height:50px; width:350px; border: solid #ff4d4d; background-color:#eff5f5; margin-top:20px; padding-left:5px;" %>
<%= x.password_field :password, :placeholder => " Password", :style => "height:50px; width:350px; border: solid #ff4d4d; background-color:#eff5f5; margin-top: 35px; padding-left:5px;" %>
<%= x.submit "Login" %>
<% end %>
</div>
<div class="divide"></div>
<div class="signup">
<h3>Signup</h3>
<%= form_for @new_user, url: {action: "create_user"} do |x| %>
<%= x.text_field :name, :placeholder => " Name", :style => "height:50px; width:350px; margin-top:20px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
<%= x.email_field :email, :placeholder => " Email", :style => "height:50px; width:350px; margin-top:35px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
<%= x.password_field :password, :placeholder => " Password", :style => "height:50px; width:350px; margin-top:35px; border: solid #ff4d4d; background-color:#eff5f5; padding-left:5px;" %>
<%= x.submit "Submit" %>
<% end %>
</div>
</div>
控制器:
class SessionsController < ApplicationController
def new
@new_user = User.new
end
def create
@user = User.find_by_email(params[:session][:email])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/'
else
render '/login'
end
end
def create_user
@new_user = User.new(user_params)
if @new_user.save
session[:user_id] = @new_user.id
redirect_to '/'
else
render '/login'
end
end
def destroy
session[:user_id] = nil
redirect_to '/login'
end
private
def user_params
params.require(:user).permit(:name, :email, :password)
end
在添加用戶資源並將新用戶和create_user操作移動到用戶控制器後,我現在正在收到參數錯誤,我認爲這是因爲我在會話的新視圖中使用'@new_user'變量,而該變量只是呈現在用戶新的和創造的行動。我該如何解決? @Ecnalyr – nums
@nums由於這是一個不同的問題,堆棧交換中的適當禮儀表明你投票正確的迴應,標記爲正確的,然後提出一個新的問題:)。 – Ecnalyr
好的,無論如何,我可以把你鏈接到新的問題來幫助解答嗎? @ Ecnalyr – nums