0

我有三个模型,SubscriptionShowEpisodeSubscription的作用是解析其:url列中的提要并实例化ShowEpisode,用 Feedjira 属性填充它们的列。

我只能在访问时访问剧集,在显示视图中shows/1/episodes/1使用不起作用并且不会给出任何错误。<%= render @show.episodes %>Shows

在 Rails 入门指南中,他们使用 this 作为他们的Commentcreate 方法,这是允许的。

@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)

是因为缺少参数导致这种情况吗?如果是这样,我将如何在此方法中使用参数?

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 %>

添加我的路线和模型以防出现问题:

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
4

1 回答 1

0

看来你只需要改变

<%= render @show.episodes %>

<%= render partial: "episodes/episode", collection: @show.episodes %>

http://guides.rubyonrails.org/action_view_overview.html#partials

和小建议:不要将你的逻辑放在控制器中。

于 2016-01-12T01:53:51.493 回答