如何在我的create方法中无法访问params的情况下将实例放在页面上?

时间:2016-01-12 01:02:36

标签: ruby-on-rails ruby feedzirra feedjira

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

我只能在访问shows/1/episodes/1时才能访问剧集,在<%= render @show.episodes %>节目视图中使用Shows无法正常工作且无法使用Comment给出任何错误。

在Rails入门指南中,他们使用此作为@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

是否缺少引起这种情况的障碍?如果是这样,我将如何在此方法中使用params?

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

.into_iter()

1 个答案:

答案 0 :(得分:0)

似乎你需要改变

<%= render @show.episodes %>

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

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

小建议:不要把你的逻辑放在控制器里。