2

我正在尝试在我的视图 add_feed 中实现 feedzirra(良好的 railcast:http ://railscasts.com/episodes/168-feed-parsing ),但我遇到了一些麻烦。我希望用户能够在网站上添加提要。我认为这应该很简单,我可以从控制台添加提要,但我还没有弄清楚如何将信息从表单传递给方法。

我的模型看起来像这样(几乎与 railscast 中的模型相同):

 def self.update_from_feed(feed_url)
  feed = Feedzirra::Feed.fetch_and_parse(feed_url)
  add_entries(feed.entries)
end

def self.update_from_feed_continuously(feed_url, delay_interval = 15.minutes)
 feed = Feedzirra::Feed.fetch_and_parse(feed_url)
 add_entries(feed.entries)
  loop do
    sleep delay_interval
   feed = Feedzirra::Feed.update(feed)
   add_entries(feed.new_entries) if feed.updated?
 end
end

private

def self.add_entries(entries)
  entries.each do |entry|
    unless exists? :guid => entry.id
      create!(
        :name         => entry.title,
        :summary      => entry.summary,
        :url          => entry.url,
        :published_at => entry.published,
        :guid         => entry.id
      )
    end
  end
end

我不太确定如何使用我的控制器和视图将字符串传递给我的 self_update_from_feed(String) 方法。我的控制器目前看起来像这样:

 def add_feed
   @feed = String
 end

def new
   @feed = Feed.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @feed }
    end
  end

  def edit
    @feed = Feed.find(params[:id])
  end

  def create
    @feed = Feed.new(params[:feed])
    @feed.user_id = current_user.id

    respond_to do |format|
      if @feed.save
        if @feed.url != nil
          @feed.update_from_feed(:url)
        end

        format.html { redirect_to @feed, notice: 'Feed was successfully created.' }
        format.json { render json: @feed, status: :created, location: @feed }
      else
        format.html { render action: "new" }
        format.json { render json: @feed.errors, status: :unprocessable_entity }
      end
    end
  end

而我的观点……嗯……

 <%= form_for @feed do |f| %>
  <%= ???%>
 <% end %>

提前感谢您的任何回复。我通常在 stackoverflow 上得到很好的帮助 :)

4

1 回答 1

2

模型

class Feed < ActiveRecord::Base
  attr_accessible :feed_url
  after_create { |feed| FeedEntry.update_from_feed(feed.feed_url) }
end

控制器

class FeedsController < ApplicationController

    def create 
        @feed = Feed.new(params[:feed])
        respond_to do |format|
          if @feed.save
            format.html { redirect_to @feed, notice: 'Feed was successfully created.' }
          else
            format.html { render action: "new" }
          end
        end    
    end

end

看法

  <%= form_for @feed do |f| %>
  <div class="field">
    <%= f.label "Feed URL" %><br />
    <%= f.text_field :feed_url %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
  <% end %>
于 2013-01-07T15:51:00.310 回答