Reputation: 119
I have three models, Subscription
, Show
, and Episode
. Subscription
's role is to parse the feed in its :url
column and instantiate Show
and Episode
, filling their columns with Feedjira properties.
I'm only able to access the episodes when I go to shows/1/episodes/1
, using <%= render @show.episodes %>
in the Shows
show view doesn't work and doesn't give any errors.
In the Rails getting started guide, they used this as their Comment
create method, which allowed for that.
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
Is it the lack of params that's causing this? If so, how would I use params in this method?
def create
@subscription = Subscription.new(subscription_params)
if @subscription.save
@show = Show.new
@episodes = []
# Feed
@feed = Feedjira::Feed.fetch_and_parse @subscription.url
# Show
@show.title = @feed.title
@show.description = @feed.description
@show.genre = @feed.itunes_categories
@show.url = @feed.url
@show.logo = @feed.itunes_image
@show.save
# Episode
@feed.entries.each do |item|
@episodes.push(item)
end
@episodes.each do |item|
@episode = @show.episodes.create
@episode.title = item.title
@episode.description = item.summary
@episode.release_date = item.published
@episode.show_id = @show
@episode.save
end
redirect_to @subscription
end
end
episodes/_episode.hmtl.erb
<ul>
<li>
<%= episode.title %>
</li>
<li>
<%= episode.description %>
</li>
<li>
<%= episode.url %>
</li>
<li>
<%= episode.release_date %>
</li>
<li>
<%= episode.show_id %>
</li>
</ul>
shows/show.html.erb
<h1>Showing Show</h1>
<h2><%= @show.title %></h2>
<%= render @show.episodes %>
Adding my routes and models in case that's the issue:
routes.rb
Rails.application.routes.draw do
resources :shows do
resources :episodes
end
resources :subscriptions
root 'subscriptions#index'
end
show.rb
class Show < ActiveRecord::Base
has_many :episodes, dependent: :destroy
end
episode.rb
class Episode < ActiveRecord::Base
belongs_to :show
end
subscription.rb
class Subscription < ActiveRecord::Base
validates :url, uniqueness: true
end
Upvotes: 0
Views: 31
Reputation: 214
Seems you need to just change
<%= render @show.episodes %>
to
<%= render partial: "episodes/episode", collection: @show.episodes %>
http://guides.rubyonrails.org/action_view_overview.html#partials
And small advice: don't place your logic in controller.
Upvotes: 0