sending variable to a javascript file in RoR - javascript

I am using ajax to call a RoR rails function and am new to this.
The function is
def destroy
#fav_company = FavouriteCompany.find(params[:id])
respond_to do |format|
format.js { render :layout=>false }
end
end
In my destroy.js.erb I have
$('#profile_alerts').show();
$('#Fav#{#fav_company.id}').hide();
The first line is working but not the second line. I am suspecting it is unable to access #fav_company.
What should I do? thanks
=====
Some additional information, the call I am making to this function is a link_to with remote => 'true' as such:
<%=link_to "destroy",{:controller=>"favourite_companies",:action=>"destroy", :id=>"#{fav.id}"}, {:remote => true } %>

This:
$('#Fav#{#fav_company.id}').hide();
Should be:
$('#Fav #{#fav_company.id}').hide();
Assuming #fav_company.id presents a variable in the DOM

Since your javascript code is in an ERB file I think you should be using $('#Fav<%=#fav_company.id%>').hide();.

Try to use:
$('#Fav#{escape_javascript({#fav_company.id}).html_safe}').hide();
==================================================================================
Ok, my code, but I does another task:
albums/show.html.haml
= link_to t('write.comment'), new_album_comment_path(#album, :format => :js, :id => #album.id), :remote => true
comments/new.js.haml
$("#comment_form").html("#{escape_javascript(render :partial => "comments/form").html_safe}");
also all code here https://github.com/barthezslavik/mebel - if you found something useful, I'll be happy

Related

Iterate through array passed from controller

I'm using Ruby on Rails.
Here is my code for the view, where I make a post to the controller, passing parameter "tplangroup.id":
<div id="collapse">
<%= form_tag(tplans_collapse_tplans_path, :method => 'post', :remote => true ) do %>
<%= hidden_field_tag(:tplangroup_id, tplangroup.id) %>
<% end %>
</div>
Here is my code on the controller end, where it parses the necessary data and shoots back array "#ordered_tplans"
def collapse_tplans
#collapsed_tplangroup = Tplangroup.find(params[:tplangroup_id])
tplans_from_tplangroup = #collapsed_tplangroup.tplans
#ordered_tplans = tplans_from_tplangroup.order("favrank DESC")
return #ordered_tplans
end
Since I called :remote => true in the original form located in the view, it passes this array to a file called "collapse_tplans.js"
My question is: what is the best way/practice to parse through this array now passed to the js file, and display its contents in the view? Do I use rails code in the js file to manipulate the object? Or do I do it all in javascript/jquery? What is the best practice, and could you provide any example?
Thanks!
Really kind of depends on how you want to go about it, as with all code, there are many ways to skin a cat. I find the easiest way is to use the return ujs as an erb file (collapse_tplans.js.erb) and from there, choose the element on the page you want to attach the retuned object to, and call a standard erb or haml partial where your iterations can be done clearly.
e.g.
In collapse_tplans.js.erb
$('#my_wacky_element').append("<%= j render(:partial => 'collapse_tplans', :locals => { :ordered_tplans => #ordered_tplans }) %>");
Then in
_collapse_tplans.html.erb
<ul>
<%= ordered_tplans.each do |tplan| %>
<li><%= tplan.attribute %></li>
Here is a RailsCast on how to pass data to js from rails:
http://railscasts.com/episodes/324-passing-data-to-javascript?view=asciicast

Rails 3 make ajax using jquery and execute .js.erb

I have the following haml code:
%input{:value => "", :type => "button",:class => "SendBtn", :onclick => "$.get('#{send_path}',{parameter:$('#parameter').val()}); "}
This input executes an event in the controller.
// This is my controller
def send
if request.xhr?
// do stuff
end
end
But my js code in the corresponding .js.erb file is not being executed. It is returned as the response of the get request.
// send.js.erb
alert('hello');
How is the rails way to have this code executed?
Your problem is not Rails related, it's jQuery. With the get method you are just fetching more or less plain text. This will not get executed. You could do an eval on the text but there is a better way. Use the getScript method from jQuery. This will fetch and execute your code.
As a side note, there are two things that are bothering me in your code:
You are using inline JavaScript. try to remove this by using a data- attribute for your send path, like this data: { sendPath: send_path }, and retrieving it with $(yourInput).data('sendPath') in your application.js file.
From my personal view I do not like to put executing JavaScript code in ERB templates. I find that this fragments the front end logic of my app. For me it worked better to put the logic in .js files and communicate with the server over JSON.
As #topek said, you have to use $.getScript. Also in your situation better approach is to use button_to with :remote => true property instead of plain input.
<%= button_to "Do Something",
{ :controller => :somecontroller, :action=> :something },
{ :remote => true }
%>
Also you can pass attributes to button_to (but you have add parameter to your route definition).
<%= button_to "Do Something",
{ :controller => :somecontroller, :action=> :something, :param => #object.id },
{ :remote => true } %>
Here goes documentation for button_to: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

How to get an argument for link_to dynamically from the site rails 3

I am using a the link_to method in Rails the unobstrusive jQuery way:
<%= link_to "save",
update_elements_diagram_path(diagram, :shown_elements => ["speed", "position_break"]),
:method => :post, :remote => true, :class => "close",
:onclick => "stringViewsTableLoading()" %>
This works. But now I want to get the arguments "shown_elements" dynamically from the users inputs.
I know how to get them from the form to javascript, but how from javascript to Rails?
... shown_elements => "getShownElements()"
or something similar would be great.
Greetings
Sandro
Not sure I see what you mean there, so I guess that you somehow have a few fields on your page, and that what you want is to get the value of those fields in the :shown_elements.
This is not how it's supposed to be done. Think the other way around. You are posting a form using :method => :post, which means that those will be accessible in your target controller using params[].
def DiagramsController < ApplicationController
def update_elements
shown_elements = params[:shown_elements] # or something like this.
end
end
Hope this helps!

jQuery/Rails 3 button does not change state

I have spent a couple of days (like 4) trying to solve this issue. I followed the Hartl Rails 3 tutorial and in chapter 12 tried to convert the site from prototype to jQuery. I am not able to get the "follow"/"unfollow" button to update however.
I am able to issue jQuery commands from within Safari's Inspect Element Console, but if I put even the simplest jQuery command into the destroy.js.erb or create.js.erb files, nothing happens. The log is indicating that the appropriate relationships/destry.js.erb (or create.js.erb) file is rendering.
Here is the code that I have in the controller:
class RelationshipsController < ApplicationController
before_filter :authenticate
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
def destroy
#user = Relationship.find(params[:id]).followed
current_user.unfollow!(#user)
respond_to do |format|
format.html { redirect_to #user }
format.js
end
end
end
The users/_follow.html.haml is
- relationship = current_user.relationships.build(:followed_id => #user.id)
= form_for(relationship, :remote => true) do |f|
= f.hidden_field :followed_id
.actions= f.submit "Follow"
The users/_unfollow.html.haml is
- relationship = current_user.relationships.find_by_followed_id(#user)
- delete = { :method => :delete }
= form_for(relationship, :html => delete, :remote => true) do |f|
.actions= f.submit "Unfollow"
The users/_follow_form.html.haml is
- unless current_user?(#user)
#follow_form
- if current_user.following?(#user)
= render 'unfollow'
- else
= render 'follow'
The relationships/create.js.erb is
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= "#{#user.followers.count} followers" %>')
The relationships/destroy.js.erb is
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= "#{#user.followers.count} followers" %>')
However, in trying to diagnose this, I tried a very basic
$("#title").html("Followed")
which also does not work.
Looks like an issue with how you're using jQuery.
jQuery uses CSS-style selectors, while Prototype doesn't. So, to make it work, just add a "#" symbol to the selectors.
Instead of
$("follow_form").html("something");
You should use
$("#follow_form").html("something");
// Note the # symbol in the selector.
// Also, remember to end your statements with a semicolon.
You can read about jQuery ID selectors here: http://api.jquery.com/id-selector/
Check your public folder for an Assets folder, with application.js in it. Not sure how I ended up with that, but that was causing ajax queries to be processed twice.
It looks like you might be using Rails 3.1. It's important to use the exact gem versions used in the tutorial (including Rails 3.0 instead of 3.1) if want the same results. See the debugging tips at the Rails Tutorial Help page for more suggestions.

Missing template after link_to_remote

I'm using link_to_remote to update a partial on my page. The problem is that Rails isn't finding my partial.
I am specifying the full path to my partial(an html.erb file) in the controller method:
def my_method
create something
render :partial => '/shared/partials/my_partial_form', :layout => 'false'
end
I know the controller method is getting hit, since "something" gets created. I get the error "Template missing [:controller_name]/[:method_name].js.erb not found".
Can anyone explain why Rails appears to be using a default path to a js.erb?
Thanks in advance!
Rails is responding to the JS format. render :partial doesn't count as the action's 1 render/redirect call per action. Without a proper render or redirect call, Rails will call render with default arguments based on the format, controller and action.
Render :partial just returns text to the caller, it does not build a response. In this case the partial is rendered into HTML, but then nothing is done with it. P.S. :layout => false option is superfluous when rendering a partial.
You want to use render :update or RJS files to do the same through a template.
Assuming you want to replace the entire web page, the short version is do something like this:
def my_method create something
render :update do |page|
page.replace_html :body, :partial => '/shared/partials/my_partial_form'
end
end
Going the RJS route you could create the app/views/resource/my_method.rjs file and fill it with
page.replace_html :body, :partial => '/shared/partials/my_partial_form'
Try removing the / in front of shared.
render :partial => 'shared/partials/my_partial_form', :layout => 'false'

Categories

Resources