为什么只有一个与嵌套路由的关联可以访问url搜索栏中所有可能的路由



尽管交易和属性之间有一对一的关联,但我对获取每个url(如deals/:id/properties/:id)的方式感到困惑。为什么当与交易2相关的属性是我数据库中的属性6时,我可以在浏览器url中键入并获取交易/2/properties/14(或任何组合)。我在视图中的链接确实有效,我使用这些链接得到了正确的关联交易/2/properties/6,但我的问题是我在设置中(或其他地方)做错了什么,还是只有Rails允许在浏览器中测试所有可能的组合。。。。如果是,有没有办法防止这种情况发生?非常感谢

我有一个一对一的关联=>1个交易有一个属性,一个属性属于交易。我有如下的嵌套路由

resources :deals, only: [:index, :show, :create, :update, :destroy] do
scope '/siteadmin' do
resources :properties
end
end
scope '/siteadmin' do
resources :deals, except: [:index, :show]
end

deal.rb

class Deal < ApplicationRecord
has_one :property, dependent: :destroy
accepts_nested_attributes_for :property
end

属性.rb

class Property < ApplicationRecord
belongs_to :deal
validates :full_address, presence: true
validates_uniqueness_of :deal_id
end

deals_controller.rb

class DealsController < ApplicationController
before_action :set_deal, only: [:show, :edit, :update, :destroy]
def index
@deals = Deal.all
end
def show
@property = @deal.property
end
def new
@deal = Deal.new
end
def create
@deal = Deal.new(deal_params)
if @deal.save
redirect_to deals_path, notice: 'Deal was successfully created'
else
render :new
end
end
def edit
end
def update
if @deal.update(deal_params)
redirect_to @deal, notice: 'Deal was successfully updated'
else
flash.now[:alert] = "Deal has not been updated."
render :edit
end
end
def destroy
@deal.destroy
redirect_to deals_path, notice: 'Deal was successfully deleted'
end
private
def deal_params
params.require(:deal).permit(:description, :kind, :address, :image_url, :occupancy, :yield)
end
def set_deal
@deal = Deal.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:alert] = "The deal you were looking for could not be found."
redirect_to deals_path
end
end

属性_控制器.rb

class PropertiesController < ApplicationController
before_action :set_deal
before_action :set_property, only: [:show, :edit, :update, :destroy]
def index
@properties = Property.all.order(id: :asc)
end
def show
end
def new
@property = @deal.build_property
end
def create
@property = @deal.build_property(property_params)
if @property.save
flash[:notice] = "Property has been created."
redirect_to [@deal, @property]
else
flash.now[:alert] = "Property has not been created."
render "new"
end
end
def edit
end
def update
if @property.update(property_params)
flash[:notice] = "Property has been updated."
redirect_to [@deal, @property]
else
flash.now[:alert] = "Property has not been updated."
render "edit"
end
end
def destroy
@property.destroy
flash[:notice] = "Property has been deleted."
redirect_to @deal
end
private
def property_params
params.require(:property).permit(:genre, :surface, :nb_rooms, :nb_bedrooms, :city, :district, :full_address)
end
def set_property
@property = Property.find(params[:id])
end
def set_deal
@deal = Deal.find(params[:deal_id])
end
end

交易展示.html.erb

<h2>Property in the deal:</h2>
<ul class="list-unstyled text-justify">
<li>Property id #<%= @deal.property.try(:id) %> - <%= link_to @deal.property.try(:full_address), [@deal, @property] %></li>
<li>Adresse of the property: <%= @deal.property.try(:full_address) %></li>
<li><%= link_to "see all the properties", deal_properties_path(@deal) %></li>
<%= link_to "add deal", new_deal_path, {class: "btn btn-primary"} %>
</ul>

属性index.html.erb

<header>
<h2>All properties</h2>
<ul id="properties in the db">
<% @properties.each do |property| %>
<li>Property id #<%= property.id %> - <%= link_to property.full_address, deal_property_path(property.deal, property) %></li>
<li><%= link_to "See our Deal", deal_path(property.deal) %></li>
<li><%= link_to "Edit Property", edit_deal_property_path(property.deal, property) %></li>
<li>Deal id #<%= property.deal.id %></li>
<% end %>
<br>
</ul>
<ul class="actions">
<li><%= link_to "Add Property", new_deal_property_path(@deal),
class: "new" %></li>
<li><%= link_to "Back to deals", deals_path%></li>
</ul>
</header>

属性show.html.erb

<header>
<h2>Property id #<%= @property.id %> <%= @property.full_address %></h2>
</header>
<h3>This is the property #<%= @property.id %> of the Deal @<%= @deal.id %> - <%= @deal.address %></h3>
<p>This property is located at <%= @property.full_address %></p>
<li><%= link_to "See all the properties", deal_properties_path(@deal)%></li>

路由只处理查询字符串的格式,而不处理其中的值。

我喜欢做的是像这个一样查看控制器中的数据

class PropertiesController < ApplicationController
before_action :set_deal
before_action :set_property, only: [:show, :edit, :update, :destroy]
def index
@properties = Property.all.order(id: :asc)
end
def show
end
def new
@property = @deal.build_property
end
def create
@property = @deal.build_property(property_params)
if @property.save
flash[:notice] = "Property has been created."
redirect_to [@deal, @property]
else
flash.now[:alert] = "Property has not been created."
render "new"
end
end
def edit
end
def update
if @property.update(property_params)
flash[:notice] = "Property has been updated."
redirect_to [@deal, @property]
else
flash.now[:alert] = "Property has not been updated."
render "edit"
end
end
def destroy
@property.destroy
flash[:notice] = "Property has been deleted."
redirect_to @deal
end
private
def property_params
params.require(:property).permit(:genre, :surface, :nb_rooms, :nb_bedrooms, :city, :district, :full_address)
end
def set_property
@property = @deal.property #.find(params[:id]) # you don't need to find it because there is only 1
end
def set_deal
@deal = Deal.find(params[:deal_id])
end
end

最新更新