我正在尝试创建一个简单的表单来创建新的租户,因为当您创建租户时,必须同时创建一个Devise用户(带有“ role:“ tenant”),但是当我提交时它会引发“路由错误”(没有路由与[POST]“ / tenants”匹配)的形式。 (我正在使用Devise和Pundit)
我有一个包含许多租户模型的财产模型
@spotify_auth_bp.route("/callback",methods=['GET','POST'])
def callback():
# Auth Step 4: Requests refresh and access tokens
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
CLIENT_ID = os.environ.get('SPOTIPY_CLIENT_ID')
CLIENT_SECRET = os.environ.get('SPOTIPY_CLIENT_SECRET')
REDIRECT_URI = os.environ.get('SPOTIPY_REDIRECT_URI')
auth_token = request.args['code']
code_payload = {
"grant_type": "authorization_code","code": auth_token,"redirect_uri": 'http://localhost/callback',}
post_request = requests.post(SPOTIFY_TOKEN_URL,data=code_payload)
# Auth Step 5: Tokens are Returned to Application
response_data = json.loads(post_request.text)
access_token = response_data["access_token"]
refresh_token = response_data["refresh_token"]
token_type = response_data["token_type"]
expires_in = response_data["expires_in"]
# At this point,there is to generate a custom token for the frontend
# Either a self-contained signed JWT or a random token
# In case the token is not a JWT,it should be stored in the session (in case of a stateful API)
# or in the database (in case of a stateless API)
# In case of a JWT,the authenticity can be tested by the backend with the signature so it doesn't need to be stored at all
# Let's assume the resulting token is stored in a variable named "token"
res = Response('http://localhost/about',status=302)
res.set_cookie('auth_cookie',token)
return res
以下是路线:
class Property < ApplicationRecord
belongs_to :owner
has_many :tenants,dependent: :destroy
validates :address,presence: true
end
class Tenant < ApplicationRecord
belongs_to :user
belongs_to :property
has_many :incidents,dependent: :destroy
validates :first_name,presence: true
validates :last_name,presence: true
validates :email,presence: true,uniqueness: true
validates :phone,presence: true
validates :rental_price,presence: true
validates :start_date,presence: true
validates :rental_time,presence: true
end
租户控制器:
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users
resources :agencies do
resources :owners,only: [:index,:new,:create]
end
resources :owners,only: [:show,:edit,:update,:destroy] do
resources :properties,only: [:new,:create]
end
resources :properties,:show,:destroy] do
resources :tenants,:create]
end
resources :tenants,:destroy] do
resources :incidents,:create]
end
resources :incidents,:destroy]
resources :workers
new.html.erb
class TenantsController < ApplicationController
before_action :set_tenant,:destroy]
def create
@tenant = Tenant.new(tenant_params)
@tenant.user = User.create(email: @tenant.email,password: random_password,role: "tenant")
@tenant.property = Property.find(params[:property_id])
if @tenant.save
redirect_to :root
else
render :new
end
authorize(@tenant)
end
def new
@tenant = Tenant.new
@tenant.user = User.new
@tenant.property = Property.find(params[:property_id])
authorize(@tenant)
end
def show
authorize(@tenant)
end
def index
@tenants = policy_scope(Tenant).order(created_at: :desc)
end
def edit
authorize(@tenant)
end
def update
authorize(@tenant)
end
def destroy
authorize(@tenant)
@tenant.destroy
redirect_back(fallback_location: root_path)
end
private
def random_password
(('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a).shuffle.first(6).join
end
def set_tenant
@tenant = Tenant.find(params[:id])
end
def tenant_params
params.require(:tenant).permit(:first_name,:last_name,:email,:phone,:rental_price,:start_date,:rental_time)
end
end
和政策
<%= simple_form_for [@property,@tenant] do |f| %>
<%= f.input :first_name,label: false,placeholder: 'Prénom' %>
<%= f.input :last_name,placeholder: 'Nom' %>
<%= f.input :email,placeholder: 'Email' %>
<%= f.input :phone,placeholder: 'Téléphone' %>
<%= f.input :rental_price,placeholder: 'Montant du loyer en euros' %>
<%= f.input :start_date,placeholder: 'Date de début de location' %>
<%= f.input :rental_time,placeholder: 'Durée de location' %>
<%= f.submit " OK " %>
<% end %>
我真的很想了解为什么它在这里不起作用,因为我为属性模型(属于所有者)制作了完全相同的东西,并且它起作用了。 谢谢!