Я смущен тем, как вы можете получить каждый URL-адрес, например, сделки /: id/properties /: id, несмотря на наличие взаимно-однозначной связи между сделкой и свойством. Почему возможно, что я могу вводить и получать сделки/2/properties/14 (или любую комбинацию) в URL-адресе браузера, когда свойство, связанное с сделкой 2, является свойством 6 в моей базе данных. Ссылки, которые у меня есть в представлениях, работают, и я получаю правильные соглашения об ассоциации/2/properties/6, используя ссылки, но мой вопрос: что-то я сделал неправильно в настройке (или везде), или это просто Rails, позволяющий всевозможные которая будет протестирована в браузере. Если да, есть ли способ предотвратить это? Большое спасибоПочему ассоциация has_one с вложенными маршрутами дает доступ ко всем возможным маршрутам в строке поиска URL?
У меня есть взаимно однозначная ассоциация => 1 сделка has_one собственность и собственность принадлежит_отделке. Я вложенные маршруты следующего
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
property.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
properties_controller.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
сделок show.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>
Спасибо Хосе теперь я могу видеть, что независимо от URL я пытаюсь напечатать это всегда будет показывать сочетание право сделки, имущество независимо url. Большое вам спасибо за то, что указали мне в правильном направлении. – nicodo
Я рад, что смог помочь –
сделано - извините, был мой первый вопрос когда-либо :) – nicodo