rubymethodsruby-on-rails-3.1controllerfeedzirra

Newbie Ruby on Rails - Pass string to method from view?


I am trying to implement the feedzirra (good railcast: http://railscasts.com/episodes/168-feed-parsing) in my view add_feed but I am having some trouble with it. I want a user to be able to add a feed while he is on the website. I think it should be pretty simple, I can add the feed from the console but I haven't figured out how to pass information to a method from a form yet.

My model looks like this (almost the same as the one from 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

I am not really sure how pass a string to my self_update_from_feed(String) method with my controller and view. My controller currently looks like this:

 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

And my view... well..

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

Thanks in advance for any response. I usually get really good help here at stackoverflow :)


Solution

  • model

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

    controller

    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
    

    view

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