2017-09-01 13 views
0

ネストされたルートとアソシエーションの操作。私はテナントを作成する部分がありますが、作成後はフォームがレンダリングされ、URLが/テナントに変わります。望ましい動作は、showページにリダイレクトする必要があることです。ユーザーがサブミットした後で、Railsがページを表示するように指示していません。

これまでのところ、プロパティとユニット(および家主)はテナントに問題があります。もともと私はユニットの下にネストされたテナントを持っていましたが、そこにも問題がありました。

<%= 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 

は最後にすくいルートでshow#テナントは次のとおりです。

tenant GET /tenants/:id(.:format)      tenants#show 
このような部分的なルックス

私は持っています広範囲にこのトピックを検索しましたが、成功しませんでした。どんな助けもありがとうございます。 5.1

答えて

0

にあなたがあなたの質問の終わり近くで見せているルートをレール:

tenant GET /tenants/:id(.:format)      tenants#show 

は、テナントの指標ではありません。それは個々のテナント/ショールートです。これには:idが含まれていますので、そのIDを持つ特定のテナントが表示されます。

rake routesをもう一度実行してください。あなたはテナントレコードを作成または更新した後のテナントのインデックスに戻りたい場合は、あなたがあなたのTenantsControllerでそのパスを指定する必要が

tenants GET /tenants(.:format)      tenants#index 

:インデックス・ルートは、次のようになります。両方#create#updateアクションでは、(それぞれif @tenant.saveif @tenant.update、後に)あなたのリダイレクトラインは次のようになります。

TenantsController、#INDEXアクションが表示されます
redirect_to tenants_path 

を。代替で

、あなたは個々のテナントショーのページに戻りたい場合は、代わりに両方#create#updateアクションでTenantsControllerのものリダイレクトの両方を変更します。

行くことができます
redirect_to tenant_path(@tenant) 

TenantsControllerには、現在の#tenantの#showアクションがあります。

+0

私はいくつかの変更を終了しました。私の主な問題はルーティングだと思う。私は浅いネストされたルートを追加し、すべてが正しく機能しているようです。あなたの助けをありがとう@moveson –

関連する問題