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 :)
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 %>
Related
I am making an app with a todo list and I recently tried to add the ability to build out the todo lists from the user.
I ran into a problem when I changed the following lines.
def new
#todo_list = current_user.todo_lists.build
end
def create
#todo_list = current_user.todo_lists(todo_list_params)
respond_to do |format|
if #todo_list.save
format.html { redirect_to #todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: #todo_list }
else
format.html { render :new }
format.json { render json: #todo_list.errors, status: :unprocessable_entity }
end
end
end
I was using the following code.
def new
#todo_list = TodoList.new
end
def create
#todo_list = TodoList.new(todo_list_params)
respond_to do |format|
if #todo_list.save
format.html { redirect_to #todo_list, notice: 'Todo list was successfully created.' }
format.json { render :show, status: :created, location: #todo_list }
else
format.html { render :new }
format.json { render json: #todo_list.errors, status: :unprocessable_entity }
end
end
end
I have added a user id to the todo items by creating a new migration and migrated the data base and created an association to the user for the posts. Not really sure what's going wrong but any help would be greatly appreciated.
Thanks.
Change this line:
#todo_list = current_user.todo_lists(todo_list_params)
into this:
#todo_list = current_user.todo_lists.build(todo_list_params)
for some reason my validation error messages are not showing on the window. does it have any to do with twitter-bootstrap? I remember seeing some video tutorial, but cannot find it now unfortunately.
_form partial of record
<%= form_for [#estate,#record] do |f| %>
<% if #record.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#record.errors.count, "error") %> prohibited this record from being saved:</h2>
<ul>
<% #record.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
.......
......
...
<% end %>
my record model
validates :E1, presence: true
validates :E2, presence: true
validates :E3, presence: true
validates :E4, presence: true
validates :R1, presence: true
validates :R2, presence: true
validates :R3, presence: true
validates :R4, presence: true
validates :Year, presence: true
validate :validation
the create and update method of the controller
def create
#record = #estate.records.build(params[:record])
respond_to do |format|
if #record.save
format.html { redirect_to [#estate,#record], notice: 'Record was successfully created.' }
format.json { render json: #record, status: :created, location: #record }
else
format.html { render action: "new" }
format.json { render json: #record.errors, status: :unprocessable_entity }
end
end
end
# PUT /records/1
# PUT /records/1.json
def update
#record = Record.find(params[:id])
respond_to do |format|
if #record.update_attributes(params[:record])
format.html { redirect_to [#estate,#record], notice: 'Record was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #record.errors, status: :unprocessable_entity }
end
end
end
does it have anything to do that record belongs to estate and estate has many records. like while creating a record i do " #record = #estate.records.build(params[:record])"
I tried debugging it using debugger, I found out that when creating a record it is not going into the else part, it is directly creating a record without checking for the validations from the record model.
Take a look at this gem http://rubygems.org/gems/dynamic_form
It will display validation errors for you in your view. You could then delete your code looping on your instance errors.
Also, nothing to do with your issue here, but I suggest refactoring your code with the following:
in your controller:
respond_to :html, :json
def create
#record = #estate.records.build(params[:record])
if #record.save
# flash[:success]... or whatever
else
# flash[:alert]... or whatever
end
respond_with #record
end
# same for your update method
For your information you can replace render action: 'new' by render :new
I'm playing about with the Ruby Feedzirra gem and have managed to acheive what I set out to do by using it in the controller.
Though everything I've seen has mentioned using it in the model. I was just wondering if this is ok to do in the controller, if not, how might I go about achieving the same in the model?
I want to submit a Feed URL and use that to update my model with the rest of the information about the feed.
Feeds_controller.rb
def create
#feed = Feed.new(params[:feed])
feed = Feedzirra::Feed.fetch_and_parse(#feed.feed_url)
#feed.title = feed.title
#feed.url = feed.url
#feed.last_modified = feed.last_modified
respond_to do |format|
if #feed.save
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
feed.rb
class Feed < ActiveRecord::Base
attr_accessible :feed_url, :last_modified, :title, :url
end
I'd make an instance method on the Model and use that in the controller so
class Feed < ActiveRecord::Base
def fetch!
feed = Feedzirra::Feed.fetch_and_parse(feed_url) # probably want some eror handling here
title = feed.title
url = feed.url
last_modified = feed.last_modified
self #or nil if you like
end
end
then your controller thins down to
def create
#feed = Feed.new(params[:feed])
#feed.fetch!
respond_to do |format|
if #feed.save
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
The cool thing here is that if this gets too slow you can use something like Resque to run this in the background and just return a "your request is being processed" message to the user, then alert them asynchronously when the the request is done (might not work so well for the json request)
You really don't want to use something like a RSS parser in your controller. That will slow the responsiveness of that URL and your server.
Instead, run the RSS parser in a separate application, or at least a separate thread, and store the retrieved feeds in a database table for rapid access.
I know, the current already asked, but I can not fix it...
Gemfile
gem 'rails', '3.2.9'
gem 'jquery-rails' , '2.0.2'
class RelationshipsController < ApplicationController
def create
#user = User.find(params[:relationship][:followed_id])
current_user.follow!(#user)
respond_to do |format|
format.html { redirect_to #user }
format.js
end
end
create.js.erb
$("#follow_form").html("<%= escape_javascript(render('shared/unfollow')) %>")
I have partial \app\views\shared\_unfollow.html.erb
<%= form_for current_user.relationships.find_by_followed_id(#account),
:html => { :method => :delete }, :remote => true do |f|%>
<div class="actions"><%= f.submit 'unfollow' %></div>
<% end %>
if press the button, the state does not change. Try add ".html_safe" doesn't work too.
But if I do
$("#follow_form").html("bla bla bla")
or
$("#follow_form").html("<%= 10+10 %>")
it's work
Try to change
$("#follow_form").html("<%= escape_javascript(render('shared/unfollow')) %>")
to
$("#follow_form").html('<%= escape_javascript(render("shared/unfollow")) %>');
Errors may be caused by invalid syntax of the javascript, so your javascript will not be executed. You will not see the erorrs in any browsers. But you can used Firefox to see the Ajax response.
I faced the similar problem before
I have a delete link that makes a remote call:
<%= link_to image_tag("trash.png"), [current_user, bookcase], method: :delete, :remote => true, confirm: "You sure?", title: bookcase.image %>
In my controller, I end the delete function with a redirect:
def destroy
#bookcase.destroy
redirect_to current_user
end
This works, except it's redirecting the user to the 'user/show.html.erb' file instead of the 'user/show.js.erb' file. How can I redirect the user, specifying which format to use?
Don't know if this is answering this specific question, but some might find the following helpful:
module AjaxHelper
def ajax_redirect_to(redirect_uri)
{ js: "window.location.replace('#{redirect_uri}');" }
end
end
class SomeController < ApplicationController
include AjaxHelper
def some_action
respond_to do |format|
format.js { render ajax_redirect_to(some_path) }
end
end
end
I'm pretty sure you can specify the format in the redirect_to like this
redirect_to current_user, format: 'js'
I am pretty sure this code will work.
render :js => "window.location = '/jobs/index'
You can use this code in action /controller_name/action name/
I tried the accepted answer but didn't work for me (RAILS 6), what worked for me is this :
format.js { redirect_to current_user }
Hope this help someone