【问题标题】:Rails not directing to show page after user submit用户提交后Rails没有指示显示页面
【发布时间】:2017-09-01 06:31:05
【问题描述】:

使用嵌套路由和关联。我有一个创建租户的部分,但在创建后它保留呈现的表单并且 url 更改为 /tenants。期望的行为是它需要重定向到显示页面。路线如下:

    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

最后,rake 路由中的 show#tenants 是:

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

我已广泛搜索此主题,但没有任何成功。任何帮助表示赞赏。导轨 5.1

【问题讨论】:

    标签: ruby-on-rails ruby forms activerecord associations


    【解决方案1】:

    您在问题接近尾声时显示的路线:

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

    不是租户索引;这是个别租户/表演路线。您可以告诉这一点,因为它包含:id,这意味着它将向您显示具有该 ID 的特定租户。

    再次尝试运行rake routes。索引路由应如下所示:

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

    如果您想在创建或更新租户记录后返回租户索引,则需要在 TenantsController 中指定该路径。在#create 和#update 操作中,您的重定向行(分别在if @tenant.save 和if @tenant.update 之后)应为:

    redirect_to tenants_path
    

    这会将您带到 TenantsController,#index 操作。

    或者,如果您想返回单个租户显示页面,则改为将 TenantsController 中 #create 和 #update 操作中的这两个重定向更改为:

    redirect_to tenant_path(@tenant)
    

    这会将您带到 TenantsController,#show 当前@tenant 的操作。

    【讨论】:

    • 我最终改变了一些东西。我认为我的主要问题是路由。我添加了一个浅嵌套路线,一切似乎都正常运行。感谢您的帮助@moveson
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多