0
我的路由問題與我的應用程序。Ruby ||路由錯誤
當我甚至嘗試查看的頁面,我得到以下錯誤
ActionController::UrlGenerationError in Conferences#index
No route matches {:action=>"create", :conference_id=>nil, :controller=>"subscriptions", :format=>nil} missing required keys: [:conference_id]
問題的原因是缺少所需的關鍵。我不知道缺少的鑰匙是什麼,我怎麼能找到它。
我相信表單的設置方式有問題,但我不知道是什麼。
有人可以告訴我,路由問題的原因是什麼?甚至可能告訴我如何解決它?
的routes.rb
resources :conferences, shallow: true do
resources :subscriptions, only: [:create, :destroy]
end
Index.html.erb
<% @conferences.each do |conference| %>
<div class="box">
<%= image_tag conference.picture.url, :class => "background" if conference.picture?%>
<div class="text-box">
<p>CONFERENCE:<%= conference.conference %>
</p>
<p>COUNTRY:<%= conference.country %>
</p>
<p>MONTH:<%= conference.month %>
</p>
<p>PRESENCE:<%= conference.presence %>
</p>
<p>AUDIENCE:<%= conference.audience %>
</p>
<p>COST:<%= conference.cost %>
</p>
<p>Attending:
</p>
</div>
</div>
<% if current_user.subscribed_to?(@conference) %>
<%= button_to "Unsubscribe", subscription_path(current_user.find_subscription(@conference)), method: :delete %>
<% else %>
<%= button_to "Subscribe", conference_subscriptions_path(@conference, @subscription), method: :post %>
<% end %>
<% end %>
ConferenceController
def index
@conferences = Conference.paginate(page: params[:page])
if params[:search]
@conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page])
else
@conferences = Conference.all.order('created_at DESC').paginate(page: params[:page])
end
end
SubscriptionController
class SubscriptionsController < ApplicationController
before_action :set_conference, only: :create
def create
if current_user.subsciptions.create(conference: @conference)
flash[:success] = "You are now subscribed to { @conference.name }"
else
flash[:error] = "Could not create subscription."
end
redirect_to @conference
end
def destroy
@subscription = current_user.subsciptions.find(params[:id])
if @subscription.destroy
flash[:success] = "You are no longer subscribed to { @conference.name }"
else
flash[:error] = "Oh noes"
end
redirect_to @subscription.conference
end
def set_conference
@conference = Conference.find(:params[conference_id])
end
end
謝謝,我對Ruby仍然很新穎:) – Salman