使用嵌套的路由和关联。我有一个创建租户的部分,但在创建之后,它会保留呈现的表单并且URL会更改为/ tenant。期望的行为是它需要重定向到显示页面。路线如下:用户提交后不直接显示页面的Rails
Rails.application.routes.draw do
devise_for :landlords
authenticated :landlord do
root "properties#index", as: "authenticated_root"
end
resources :tenants
resources :properties do
resources :units
end
root 'static#home'
end
到目前为止,性质和单位的工作(与房东)问题是租户。原来我有租户嵌套在单位下,但也有问题。部分看起来像这样:
<%= form_for @tenant do |f| %>
<%= f.label "Tenant Name:" %>
<%= f.text_field :name %>
<%= f.label "Move-in Date:" %>
<%= f.date_field :move_in_date %>
<%= f.label "Back Rent Amount:" %>
$<%= f.text_field :back_rent %>
<%= f.button :Submit %>
<% end %>
<%= link_to "Cancel", root_path %>
租户控制器看起来是这样的:
before_action :authenticate_landlord!
#before_action :set_unit, only: [:new, :create]
before_action :set_tenant, except: [:new, :create]
def new
@tenant = Tenant.new
end
def create
@tenant = Tenant.new(tenant_params)
if @tenant.save
redirect_to(@tenant)
else
render 'new'
end
end
def show
end
def edit
end
def update
if @tenant.update(tenant_params)
redirect_to unit_tenant_path(@tenant)
else
render 'edit'
end
end
def destroy
end
private
def set_property
@property = Property.find(params[:property_id])
end
def set_unit
@unit = Unit.find(params[:unit_id])
end
def set_tenant
@tenant = Tenant.find(params[:id])
end
def tenant_params
params.require(:tenant).permit(:name, :move_in_date, :is_late, :back_rent, :unit_id)
end
end
型号有关联:
class Tenant < ApplicationRecord
belongs_to :unit, inverse_of: :tenants
end
class Unit < ApplicationRecord
belongs_to :property, inverse_of: :units
has_many :tenants, inverse_of: :unit
end
最后的抽佣路线秀#租户是:
tenant GET /tenants/:id(.:format) tenants#show
我有广泛搜索这个话题,但没有取得任何成功。任何帮助表示赞赏。 Rails的5.1
我最终改变了一些东西。我认为我的主要问题是路由。我添加了一个浅层嵌套路线,并且所有内容都似乎正常工作。感谢您的帮助@moveson –