0

我正在尝试在 Property#show 视图中添加租户用户。我有一个表格,如下所示:

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

当我单击添加按钮时,我被神奇地重定向到根路径 (/)。我希望它为我在显示视图中查看的特定属性添加一个新租户,但它只是将我重定向到根路径。

随时询问 rake 路线的任何澄清结果:

  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
4

2 回答 2

0

在你的 properties_controller.rb,

def show
 @tenant = Tenant.new
end

你的 from 应该是:

<%= 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 约定将其移至租户控制器,并将表单置于新操作下,因为您实际上是在创建新的租户记录。

于 2013-02-14T18:25:42.683 回答
0

我认为有几件事似乎不太正确。

尝试这个。

将 @tenant 实例变量添加到 Properties 控制器的 show 方法中。

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

并将 form_for 行修改为此。

<%= form_for @tenant do |f| %>
于 2013-02-14T18:18:03.490 回答