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