I have a link that needs to show a hidden Div on click.
For it, I am using :
<%=link_to video.title,display_path(:format=>:js,:id=>video.id),:remote=>true%>
in the controller i have:
def display
#video=Video.find(params[:id])
respond_to do |format|
format.js
end
end
and in the js.erb, i have,
$('#vid_<%=#video.id%>').fadeIn('slow');
But when i run the app, upon clicking the link, the js.erb is rendered as text.It just displays:
$('#vid_<%=#video.id%>').fadeIn('slow');
You should read
Rails Ajax: .js.erb rendered as text, not JS
for a full explaination
You are doing everything right, but you can improve your code a little
views
# use spaces to ident all this stuff and write less if it is possible:
<%= link_to video.title, [:display, video], :remote => true %>
controller
class VideosController < ApplicationController
respond_to :html, :js
...
def display
#video=Video.find(params[:id])
respond_with #video
end
...
end
display.js.erb
$('#vid_<%= #video.id %>').fadeIn('slow');
Be sure, that you have installed jQuery:
rails g jquery:install
and included it into your application layout file.
<%= javascript_include_tag :defaults %>
Ok I found the solution.I made a silly mistake.I forgot to skip prototype while creating the App.This caused trouble when i tried to use Jquery.Its all good now.
Related
I'm having an issue with UJS, jQuery and partials. I have a table with a bunch of rows. When I click edit on one of the rows, the row transforms into text fields where I can edit the row. Then I have a submit; however, clicking it does nothing. It just doesn't look like its triggering. Clicking edit sends a call to the controller, which responds with edit.js.erb which has jQuery replace the contents of the row with the edit partial.
Is there anything obvious I'm overlooking? I'd post code, but am on my phone and can't get online on the computer. I'll edit later if I can't figure it out. I was hoping for some feedback in the meantime.
Thanks
Ok, here's some code:
Item controller:
def edit
respond_to do |format|
format.html { respond_with #item }
format.js { }
end
end
def update
#item.update_attributes(params[:item])
respond_to do |format|
format.html { respond_with #item, :location => items_path }
format.js { }
end
end
edit.html.erb:
<%= form_for(#item, item_path) do |form| %>
<%= render form %>
<% end %>
edit.js.erb:
$(' .item ').html(" 'item/edit', :locals => {:item => #item})) %>");
update.js.erb:
$(' .item ').html(" 'item/item', :item => #item) %>");
It actually ended up being an issue with html closing my form tag prematurely. I didn't know you couldn't put a form in a table cell. I had stared at the source for so long and couldn't see what was wrong.
If this is happening to you, make sure you check where your closing form tag is.
Rails 3.2.1: I have the following div that calls a partial
<div id="weighin">
<%= render :partial => "my_weight/weighin" %>
</div>
The partial contains a form that posts ajax (ie has :remote => true) to a controller with:
respond_to do |format|
format.js
end
The .js.erb file has a single line:
$("#weighin").html("<%= render :partial => "my_weight/weighin1" %>");
The _weighin1.html.erb partial file has a single line:
<p><%= #my_weight[1].weight %></p>
This works, in that the original div is replaced with the value of the #my_weight field - so the fundamental structure is all working ok
However, Rails will not handle any more code in the partial - if I add so much as a carriage return to the end of that one line, the server log confirms all ok, but no script gets run on the page - ie nothing changes.
The same happens if I try to put more html in the partial, but put it all in a single line - this doesnt run either.
How can I output more than a single short statement in a partial?
OK, I figured this out:
$("#weighin").html("<%= escape_javascript(render :partial => "my_weight/weighin1") %>");
escape_javascript is essential
What is confusing is that, depending on what is in the html() you are sending, this will sometimes work without escape_javascript, leading one to a false conclusion... :-)
I'm learning to program and got a form running in my Rails 3 app. Now I'm attempting to add ajax to the form so the page doesn't reload after submitting.
I've followed the numerous tutorials but can't quite seem to figure out how to bring it together. The form adds new Objects to the Profile through the following model:
class Profile < ActiveRecord::Base
has_many :objects
end
class Object < ActiveRecord::Base
belongs_to :profile
end
My form in views/profiles/_object_form.html.erb:
<%= form_for(#object, :remote => true) do |f| %>
<% end %>
Where the form and its created objects are rendered in my views/profiles/_about.html.erb:
<div id="newObjects">
<%= render :partial => 'object_form' %>
</div>
<div id="objectList">
<%= render :partial => 'object', :collection => #profile.objects, :locals => {:object_count => #profile.objects.length) %>
</div>
In my objects_controller.rb I have the following create action:
def create
#object = Object.new(params[:object].merge(:author_id => current_user.id))
respond_to do |format|
if #object.save!
format.html {redirect_to profile_path(#object.profile) }
format.js { render }
else
format.html { redirect_to #profile, :alert => 'Unable to add object' }
end
end
end
In views/objects/create.js.erb:
$('#objectList').append("<%= escape_javascript(render #profile.object)) %>");
So I have a form calling an action in another controller to which I want to add ajax. What happens at the moment is that I need to reload the profile to show the newly created object. What am I doing wrong?
CLARIFICATION: Other than the create action in the ObjectsController, I only reference #object once elsewhere. That's in the ProfilesController's show action:
def show
#profile = Profile.find(params[:id])
#superlative = #profile.superlatives.new`
end
Not sure if this is a full code snippet for your create action, but looks like you are trying to call render on an instance variable that doesn't exist... #profile is never set in the create method in the ObjectController...
Perhaps you meant to type $('#objectList').append("<%= escape_javascript(render #object)) %>");
Also noticed that in your existing code you're making a call to render #profile.object, but the Profile class has a has_many relationship with your Object class, so if that was the right code, then you should type render #profile.objects (plural, not singular).
But I would think you would likely want the code I mentioned above, since you are appending onto the list of objects, not rendering the list again?
I am trying to create a custom POST action for my article object.
In my routes.rb, I have set the action in the following way:
resources :articles do
member do
post 'update_assigned_video'
end
end
In my articles_controller.rb I have:
def update_assigned_video
#article = Articles.find(params[:id])
#video = Video.find(:id => params[:chosenVideo])
respond_to do |format|
if !#video.nil?
#article.video = #video
format.html { redirect_to(#article, :notice => t('article.updated')) }
else
format.html { render :action => "assign_video" }
end
end
Then in my view I make a form like this:
<%= form_for #article, :url => update_assigned_video_article_path(#article) do |f|%>
[...]
<%= f.submit t('general.save') %>
The view renders (so I think he knows the route). But clicking on the submit button brings the following error message:
No route matches "/articles/28/update_assigned_video"
rake routes knows it also:
update_assigned_video_article POST /articles/:id/update_assigned_video(.:format) {:action=>"update_assigned_video", :controller=>"articles"}
What am I doing wrong?
Is this the wrong approach to do this?
Your form_for will do a PUT request rather than a POST request, because it's acting on an existing object. I would recommend changing the line in your routes file from this:
post 'update_assigned_video'
To this:
put 'update_assigned_video'
I'm upgrading from Rails 2.3.8 to 3.0.3 and notice that my code for nested layouts isn't working.
In my main Application layout I have the line
<%= controller.sub_layout %>
which then looks to the controller, who has:
def sub_layout
render :inline => "<%= yield %>"
# or otherwise some partial for the sub-layout
end
The problem is, this doesn't get rendered! If I put a direct <%= yield %> statement in the layout, it does work. So the question is, what's happening here, and how do I fix it?
This worked beautifully in Rails 2.3.8
How about a much saner approach:
render :layout => false
Related: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
So you all have some more of a background on this, the whole sub-layout approach was based on this blog post: Sorta Nested Layouts (The solution is given in the comments section.)
Instead of making a controller method sub_layout, any controller that uses a sublayout needs to define a before_filter method that sets a variable:
def inner_layout
#inner_layout = 'layouts/sublayout_partial_name'
end
then in the main layout.html.erb (i.e. application.html.erb), where you would otherwise put your yield statement:
<%= #inner_layout ? render(:partial => "#{#inner_layout}") : yield %>
the assumtion is that the sublayout partial file will have its own yield statement in there somewhere.