2013-02-14 33 views
0

我試圖在房產#顯示視圖內添加租戶用戶。我有一個表格有類似如下:在房產視圖中添加租戶用戶

<%= form_for(@property.tenants) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="select"> 
    <%= f.select :type, [['Landlord'],['Tenant']] %> 
    </div> 
    <%= f.submit "Add", %> 
<% end %> 

租客模型如下:

class Tenant < User 
    belongs_to :property 
    def type 
    "Tenant" 
    end 
end 

和屬性模型如下:

class Property < ActiveRecord::Base 
    attr_accessible :address, :bathrooms, :bedrooms, :postcode, :price 
    belongs_to :branch 
    belongs_to :user 

    has_many :ownerships 
    has_many :landlords, :through => :ownerships 

    has_many :tenants 
end 

當我點擊添加按鈕我神奇地重定向到根路徑(/)。 我期待它爲我在show view中查看的特定屬性添加新租戶,但它只是將我重定向到根路徑。

隨意問任何澄清 結果耙路線:

tenants_index GET /tenants/index(.:format)  tenants#index 
    tenants_show GET /tenants/show(.:format)  tenants#show 
    tenants_new GET /tenants/new(.:format)   tenants#new 
    landlords_new GET /landlords/new(.:format)  landlords#new 
    tenants_edit GET /tenants/edit(.:format)  tenants#edit 
tenants_create GET /tenants/create(.:format)  tenants#create 
tenants_update GET /tenants/update(.:format)  tenants#update 
tenants_destroy GET /tenants/destroy(.:format)  tenants#destroy 
    properties GET /properties(.:format)   properties#index 
       POST /properties(.:format)   properties#create 
    new_property GET /properties/new(.:format)  properties#new 
    edit_property GET /properties/:id/edit(.:format) properties#edit 
     property GET /properties/:id(.:format)  properties#show 
       PUT /properties/:id(.:format)  properties#update 
       DELETE /properties/:id(.:format)  properties#destroy 
      users GET /users(.:format)    users#index 
       POST /users(.:format)    users#create 
     new_user GET /users/new(.:format)   users#new 
     edit_user GET /users/:id/edit(.:format)  users#edit 
      user GET /users/:id(.:format)   users#show 
       PUT /users/:id(.:format)   users#update 
       DELETE /users/:id(.:format)   users#destroy 
     companies GET /companies(.:format)   companies#index 
       POST /companies(.:format)   companies#create 
    new_company GET /companies/new(.:format)  companies#new 
    edit_company GET /companies/:id/edit(.:format) companies#edit 
     company GET /companies/:id(.:format)  companies#show 
       PUT /companies/:id(.:format)  companies#update 
       DELETE /companies/:id(.:format)  companies#destroy 
     branches GET /branches(.:format)   branches#index 
       POST /branches(.:format)   branches#create 
    new_branch GET /branches/new(.:format)  branches#new 
    edit_branch GET /branches/:id/edit(.:format) branches#edit 
     branch GET /branches/:id(.:format)  branches#show 
       PUT /branches/:id(.:format)  branches#update 
       DELETE /branches/:id(.:format)  branches#destroy 
     sessions POST /sessions(.:format)   sessions#create 
    new_session GET /sessions/new(.:format)  sessions#new 
     session DELETE /sessions/:id(.:format)  sessions#destroy 
     agents GET /agents(.:format)    users#index 
       POST /agents(.:format)    users#create 
     new_agent GET /agents/new(.:format)   users#new 
    edit_agent GET /agents/:id/edit(.:format)  users#edit 
      agent GET /agents/:id(.:format)   users#show 
       PUT /agents/:id(.:format)   users#update 
       DELETE /agents/:id(.:format)   users#destroy 
     landlords GET /landlords(.:format)   users#index 
       POST /landlords(.:format)   users#create 
    new_landlord GET /landlords/new(.:format)  users#new 
    edit_landlord GET /landlords/:id/edit(.:format) users#edit 
     landlord GET /landlords/:id(.:format)  users#show 
       PUT /landlords/:id(.:format)  users#update 
       DELETE /landlords/:id(.:format)  users#destroy 
     tenants GET /tenants(.:format)    users#index 
       POST /tenants(.:format)    users#create 
    new_tenant GET /tenants/new(.:format)   users#new 
    edit_tenant GET /tenants/:id/edit(.:format) users#edit 
     tenant GET /tenants/:id(.:format)   users#show 
       PUT /tenants/:id(.:format)   users#update 
       DELETE /tenants/:id(.:format)   users#destroy 
      root  /       static_pages#home 
     signup  /signup(.:format)    users#new 
     signin  /signin(.:format)    sessions#new 
     signout DELETE /signout(.:format)    sessions#destroy 
     dashboard  /dashboard(.:format)   static_pages#dashboard 
      help  /help(.:format)    static_pages#help 
      about  /about(.:format)    static_pages#about 
     contact  /contact(.:format)    static_pages#contact 

properties_controller.rb

class PropertiesController < ApplicationController 
    # GET /properties 
    # GET /properties.json 
    def index 
    @properties = Property.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @properties } 
    end 
    end 
def show 
    @property = Property.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @property } 
    end 


    end 

    # GET /properties/new 
    # GET /properties/new.json 
    def new 
    # @property = Property.new 
    @property = current_user.properties.build if signed_in? 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @property } 
    end 
    logger.debug("hello from new") 
    end 

    # GET /properties/1/edit 
    def edit 
    @property = Property.find(params[:id]) 
    end 

    # POST /properties 
    # POST /properties.json 
    def create 
    #@property = Property.new(params[:property]) 
    @property = current_user.branch.properties.build(params[:property]) if signed_in? 
    respond_to do |format| 
     @property.user_id = current_user.id 
     if @property.save 
     format.html { redirect_to @property, notice: 'Property was successfully created.' } 
     format.json { render json: @property, status: :created, location: @property } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @property.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 
+0

通過在您的終端中執行'rake routes'向我們展示您的路線。同時向我們顯示您的控制器已添加屬性 – 2013-02-14 17:51:25

+0

。看問題! – bytebiscuit 2013-02-14 18:09:55

回答

0

我覺得有幾件事情,似乎並不完全正確。

試試這個。

將@tenant實例變量添加到Properties控制器的show方法中。

def show 
    ... 
    @tenant = @property.tenants.build 
end 

並修改form_for行爲此。

<%= form_for @tenant do |f| %> 
0

在你properties_controller.rb

def show 
@tenant = Tenant.new 
end 

你再從應該是:

<%= form_for @tenant do |f| %> 

您還應該添加一個隱藏字段屬性的ID,因爲當你提交表單。

然後爲您創建行動properties_controller.rb

def create 
    tenant = Tenant.new(params[:tenant]) 
    tenant.property_id = params[:property_id] #This is from that hidden field with your property_id and this also assumes you have a column in your tenant table for property_id 
    tenant.save 

    #the shorter way would just be tenant = Tenate.create(params[tenant]) but I want to emphasize the property_id since this attribute is paramount to your property-tenant relationship 
end 

這將有效地創建一個表單新房客,當你創建實際的承租人記錄,你會救樓市ID與新承租人記錄。

在另一個說明中,我建議將此按照Rails約定移至租戶控制器,並在新操作下創建表單,因爲您確實正在創建新的租戶記錄。

相關問題