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
Related
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 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 %>
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
I have a form that works on html, I am trying to convert that to ajax. Here is my code, I think the issue is that my form doesn't submit an ajax request, it is still sending normal html request. I get rendered a new page (item/create) with the partial. How do fix this?
<%= form_tag ( :action => :create, :remote => true, :method => :post ) do %>
... #stuff
<% end %>
Controller -
def create
...
respond_to do |format|
format.html { render :partial => 'view_item', :item_num => #t[:item_num], :flag => "new" }
format.js
end
end
JS -
# cat view.js.erb
$('#item').html(<%= (render :partial => 'view_item').to_json.html_safe %>); #I have "item" div in my view
Log -
Started POST "/item/create" for 10.10.23.12 at 2012-04-18 06:41:23 -0400
Processing by ItemController#create as HTML
Parameters: {"utf8"=>"รข", "authenticity_token"=>"GRNJAx2ePzRIOaJjr2w4F6WwueRq7FL9tIDaKtIPZSQ=",
.......#long log here
"
(0.1ms) COMMIT
Rendered item/_view_item.html.erb (1.6ms)
Completed 200 OK in 3261ms (Views: 2.4ms | ActiveRecord: 4.1ms)
I'm getting a 500 error when I try to use ajax to delete a post. It works just fine without using ajax.
In the view I have this to delete a post
<%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete,:remote => true, :class => 'delete_post' %>
In the controller I have this for the Destroy method.
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js
end
end
In the browser I get a 500 error.
Run Rails 3.1 Ruby 1.9.2-p290 and brand new 3.1 app
What am I doing wrong
It's probably a missing template error. If you don't specify any parameters in a format statement, Rails looks for and loads a file named action . format . template language (destroy.js.erb).
Try something like this:
format.js { render text: "Object successfully destroyed", status: :destroyed }