How to pass parameter to js.erb in Rails? - javascript

I want to an ajax request to call user controller's xyz action with the passed parameter stage and then append it on #the_box so that I can perform conditional operation like for stage x append #user.age, for stage y append #user.age+1 and for stage z append #user.age-1. How should I do?
I have a html.erb file contains such code:
<center>
<%= link_to "A", xyz_user_path, stage: "x", remote: true %>
<%= link_to "B", xyz_user_path, stage: "y", remote: true %>
<%= link_to "C", xyz_user_path, stage: "z", remote: true %>
</center>
<div id="the_box"></div>
and users_controller.rb has
before_action :set_user, only: [ :xyz]
def xyz
respond_to do |format|
format.js {}
end
end
private
def set_user
#user = User.find(params[:id])
end
and this xyz.js.erb is what I have now but cannot deal with parameter so it cannot handle conditional operation
$("#the_box").append("<%= #user.age%>");

The other answers will help you on here - let me explain them
format.js
When you use format.js to filter the mime types, opening brackets basically tells Rails that you want to perform some custom functionality (and hence it won't load the [action].js.erb as default)
If you want to just invoke [action].js.erb, you'll have to call the following:
respond_to do |format|
format.js #-> will just call [action].js.erb
end
You'll want to read up more about Rails mime types (how to determine XHR requests) & the respond_to block
#instance_variables
Secondly, you need to consider the role of #instance variables in your application, and consequently, the method in question.
The main issue you have is you're calling #user in a method which doesn't define it - hence you'll likely see the error undefined method ___ for nil:NilClass or similar
You must remember that Rails is just a gem - a series of classes which runs on the Ruby language. This means that each time you wish to perform a method on an "object" / piece of data, you'll have you to declare / create the object in your Rails application:
def xyz
#user = User.find params[:id] #-> sets the variable for the duration of the "instance"
...
end
Please take note of the name of the variable -- an instance variable. By setting #user (instance variables are always defined with an #), you basically give Rails the ability to access the data inside it for as long as the class which defines the variable is invoked.
Basically means that if you set #user = User.find, it will be available through your view & other helper methods called from the instance of the class
Fix
Finally, to address your question directly:
I want to an ajax request to call user controller's xyz action with
the passed parameter stage and then append it on #the_box so that I
can perform conditional operation like for stage x append #user.age,
for stage y append #user.age+1 and for stage z append #user.age-1. How
should I do?
#config/routes.rb
resources :users do
get "xyz/:stage" #-> domain.com/users/:user_id/xyz/:stage
end
#app/views/users/your_view.html.erb
<% links = [["A", "x"], ["B", "y"], ["C", "z"]] %>
<% links.each do |link, stage| %>
<%= link_to link, xyz_user_path(user, stage: stage), remote: true %>
<% end %>
#app/controllers/users_controller.rb
Class UsersController < ApplicationController
def xyz
case params[:stage]
when "x"
#user.increment!(:age)
when "y"
#user.decrement!(:age)
end
respond_to do |format|
format.js
end
end
end
#app/views/users/xyz.js.erb
$("#the_box").append("<%=j #user.age %>");

You should pass the parameter in the helper like this:
xyz_user_path(stage: "x")
And then in your js.erb you can access it like:
<%= params['stage'] %>

Here I'm going to access an instance variable defined in wish_me action.
controllers/jserb_controller.rb code:
class JserbController < ApplicationController
def index
end
def wish_me
#test = 'Hi how are you?'
respond_to do |format|
format.js
end
end
end
/views/jserb/index.html.erb code:
<div class="wish-me"></div>
<%= link_to "Wish Me", wishme_path, remote: true %>
/views/jserb/wish_me.js.erb
$(".wish-me").append("<%=j #test %>");
config/routes.rb code:
match '/index' => 'jserb#index'
match '/wishme' => 'jserb#wish_me', :as => :wishme
Now try to hit http://<domain-name>/index and then click on WishMe
link. So now you are able to pass instance variable to js.erb and accessed the same.

Related

Rails AJAX request to controller method that then calls javascript function

I have looked at dozens of stack overflow posts and haven't found a solution that works which is why I'm reaching out for an otherwise well documented use case.
I have a button that that should do this.
When clicked, call a custom controller method that updates the model and does other things
Call a javascript function to update the page without reloading it (ajax)
Right now, the only way I am able to call a custom controller method is via this way which feels really hacky. I have stripped this down to as simple as possible.
routes.rb
match 'admin/:id/toggleAdmin' => 'admin#toggleAdmin', via: [:patch, :put], as: :toggleAdmin
list.html.erb
<td><%= link_to "Toggle Admin", toggleAdmin_path(id: user.id), method: :patch %></td>
admin_controller.rb
class AdminController < ApplicationController
def toggleAdmin
idToToggle = User.find(params[:id]).id
if idToToggle == current_user.id
redirect_to admin_list_path, danger: "You tried to make yourself a normal user! Don't do that!"
else
User.find(params[:id]).updateToAdmin()
redirect_to admin_list_path, info: "The user with an ID of #{idToToggle} has had their admin attribute toggled!"
end
end
end
What I would like to do instead of reloading the page when an admin is toggled is to just use some javascript to rewrite that part of the dom.
What is a better way to go about this?
Here are just a few of the various resources I have already tried.
How to call a controller's method from a view?
Can we call a Controller's method from a view (as we call from helper ideally)?
Call controller method with rails-Ajax?
https://www.reddit.com/r/rails/comments/7iyp6g/want_a_button_to_call_a_method_apparently_this_is/
How do I call a JavaScript function from an html.erb
Thanks for the help.
--- Edit for more information.
Using a clean rails app I now have the ability to call a controller method more cleanly but I am not getting an ajax request to go through that updates the page to show that the action was completed. (I am expecting a boolean value change and a flash). Here is the following relevant code:
users.js
$("#edit-form").html("<%= j render partial: 'form', locals: { user: #user } %>")
_form.html.erb
<%= form_for user do |form| %>
<%= user.admin %>
<% end %>
<%= link_to "Toggle Admin", toggle_admin_user_path(user), method: :put, remote: true %>
edit.html.erb
<h1>Editing User</h1>
<div id="edit-form">
<%= render partial: 'form', locals: { user: #user } %>
</div>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', users_path %>
users_controller.rb Note that I have only included the toggle_admin and a couple other methods as the rest are just scaffolding.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy, :toggle_admin]
def toggle_admin
if 1 == 1
logger.info "This is from info"
u = User.find(params[:id])
u.admin = !(u.admin)
u.save
respond_to do |format|
format.js { flash[:info] = "The user with an ID of #{#user.id} has had their admin attribute toggled!" }
end
else
redirect_to admin_list_path, danger: "You tried to make yourself a normal user! Don't do that!"
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.fetch(:user, {})
end
end
The routing
First thing, if you need a nested custom route inside a resource you can define it as a member of this resource:
resources :users do
put 'toggle_admin', on: :member
end
which returns:
toggle_admin PUT /users/:id/toggle(.:format) users#toggle
Second, your method names should be in snake_case : def toggle_admin
See "Instance Methods" section
The controller
If you just want to update a user to admin (guessing by this method here: User.find(params[:id]).updateToAdmin()), you can define the custom toggle_adminmethod inside the UsersController.
Your custom method for triggering ajax request should respond to the js format and point to the corresponding js view (toggle_admin.js.erb)
As a member of the resource User, you can add it to the before_action callback to return the right user when you call the method.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy, :toggle_admin]
#...
def toggle_admin
if #some condition
# do some logic here
respond_to do |format|
format.js { flash[:info] = "The user with an ID of #{#user.id} has had their admin attribute toggled!" }
end
else
redirect_to admin_list_path, danger: "You tried to make yourself a normal user! Don't do that!"
end
end
end
The view
In rails views, when you want to use ajax to update a view without reload, you need to use partial. It's this partial which will be refreshed inside the .js.erb file on a specific DOM element.
Let's make an example for the user#edit view.
First you need to render the view inside a partial form for example, and wrap it into a div with specific id:
edit.html.erb
<div id="edit-form">
<%= render partial: 'form', locals: { user: #user } %>
</div>
The partial:
_form.html.erb
<%= form_for user do |form| %>
# the form
<% end %>
<%= link_to "Toggle Admin", toggle_admin_user_path(user), method: :put, remote: true %>
# don't forget the remote: true for the ajax call
Finally the .js.erb for the custom action (assuming you have jquery):
toggle_admin.js.erb
$("#edit-form").html("<%= j render partial: 'form', locals: { user: #user } %>")
And voilĂ  ! when click the link, you refresh the edit view with new informations from the controller without reloading the view.
Of course, this is an example for a classic resource's edit view but you can adapt it for any case. Just keep the good naming between routes, controllers, controller methods and views (AdminController, list view etc...)
Edit
To handle the flash messages, you need to add inside your layout > application.html.erb :
<body>
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "classname" %>
<% end %>
<%= yield %>
</body>

Controller testing with js.erb format

I'm using Rails 4.2.5, I've written a controller test for the destroy action, an I'm using ajax call to destroy and using destroy.js.erb file. Please help me to solve the following issue to pass the test when it calls js format, I'm pasting error below.
def destroy
#status = #song.destroy
respond_to do |format|
format.js
end
end
SongsControllerTest#test_should_destroy_song:
ActionController::UnknownFormat: ActionController::UnknownFormat
app/controllers/songs_controller.rb:36:in `destroy'
songs_controller_test.rb
test "should destroy song" do
assert_difference('Song.count', -1) do
delete :destroy, id: #song
end
get :destroy, format: 'js'
end
destroy.js.erb
var element = document.getElementById("<%="song#{#song.id}" %>");
<%if #status%>
element.parentNode.parentNode.remove();
<%end%>
The destroy controller action normally reacts to the DELETE HTTP method, so you should use the format option when calling delete:
test "should destroy song" do
assert_difference('Song.count', -1) do
delete :destroy, id: #song, format: :js
end
end

Convert working Ruby on Rails form to AJAX

I have a working Ruby on Rails form that currently posts my results and redirects the user to another view and posts the results. The flow is:
views/tools/index.html.erb -> views/tools/ping.html.erb
Since I have it working now, I'd like to convert it to AJAX and keep the user on the views/tools/index.html.erb view, getting rid of the redirect to enhance the user experience. However, I'm unsure of how to proceed based on the way that my Tools controller is currently setup, and my incredibly lacking knowledge of AJAX.
So, here's what I currently have:
views/tools/index.html.erb (added 'remote: true' to form)
<h1> Tools </h1>
<h3> Ping </h3>
<%= form_tag ping_tool_path(1), method: "post", remote: true do %>
<%= text_field_tag :ip, params[:ip] %>
<%= submit_tag "Ping", name: nil %>
<% end %>
<!-- this is where I'd like to put the results via AJAX -->
<div id="output"></div>
controllers/tools_controller.rb
class ToolsController < ApplicationController
def index
end
def ping
ping_host(params[:ip])
save_host(params[:ip])
# Adds based on recommendations
respond_to do |format|
format.html { redirect_to tools_path }
format.js
end
end
protected
def ping_host(host)
f = IO.popen("ping -c 3 #{host}")
#output = f.readlines
tool_type = "ping"
tool = Tool.find_by(tool_type: tool_type)
tool.increment(:tool_hit_count, by = 1)
tool.save
#results = "<pre>#{#output.join}</pre>".html_safe
end
def save_host(host)
host = Host.find_or_create_by(host_ip: host)
host.increment(:host_hitcount, by = 1)
host.save
end
end
views/tools/ping.html.erb
<%= #results %>
views/tools/ping.js.erb (New file based on suggestion)
$("#output").html("<%= #results %>");
routes.rb
Rails.application.routes.draw do
root 'tools#index'
resources :tools do
member do
post 'ping'
end
end
end
This is what I see on the Network tab in Google Chrome after submitting the form:
So, what I know at this point is that I'll need to add remote: true to my form in views/tools/index.html.erb, and this is where I get lost.
It seems that I have an issue ATM with the fact that I've abstracted the form to use my ping method in the Tools controller, whereas all of the tutorials (and railscasts) I've gone through are doing AJAX on CRUD methods and a given model, not something like what I've build here so far. Please help me understand AJAX!
You're on the right track, now you need to modify the def ping action with a respond_to block.
def ping
ping_host(params[:ip])
save_host(params[:ip])
respond_to do |format|
format.html { redirect_to tools_path } ## if you still want to have html
format.js
end
end
and create a file called view/tools/ping.js.erb where you have javascript that will be run and returned asynchronously
$("#output").html("<%= j #results %>");
The <%= %> block will be evaluated first and replaced with the output of that ruby code. Then this will be inserted into the #output div.

Pass select value to controller without refreshing the page (AJAX, RAILS, JS)

I'm a newbie using AJAX and now I'm trying to work on a group of select boxes so that when the user selects a value from the first, the second updates its values. I'm trying to use AJAX, JQuery and ROR, but so far, I can't fetch the value from the first box and pass it to the controller in order to filter the results of the second.
This is my code for index.html.erb
<%= form_tag(posts_path, remote: true, :method => 'get', class: "forma") do %>
<%= select_tag("box", options_from_collection_for_select(#posts, :content, :content), :prompt => "Select") %>
<p>
<%= select_tag("nbox", options_from_collection_for_select(#listposts, :content, :id), :prompt => "Select") %>
<p>
<%= submit_tag "Show", class: "btn btn-info" %>
<% end %>
This is my index.js.erb
$(document).ready(function() {
$(".btn").on("click", function() {
$('.table').fadeToggle();
})
$('#box').change(function(){
$.ajax({
type: 'POST',
url: '/jess/rails/dummy/app/controllers/posts_controller',
data: {'filter' : $(this).val() },
success: function(data){
}
})
$('#nbox').fadeIn();
$('.btn').fadeIn();
})
})
This is my controller
class PostsController < ApplicationController
respond_to :html, :js
def new
#post = Post.new
end
def create
#posts = Post.all
#post = Post.new(post_params)
if #post.save
flash.now[:notice] = "YEAH"
respond_to do |format|
format.html {redirect_to #post.content}
format.js
end
else
render "/"
end
end
def show
#post = Post.find(params[:id])
end
def index
#posts = Post.all
#listposts = Post.where('content LIKE ?', params[:box])
end
def next
#listposts = Post.where('content LIKE ?', params[:box])
end
private
def post_params
params.fetch(:post, {}).permit(:content)
end
end
I think I'm doing it wrong with the AJAX part in the js.erb but I've changed it so many times I don't know anymore. Your help would be so very much appreciated!!
It looks like there are a couple of small problems.
First, it looks like the URL you are using may be wrong. The URL for the AJAX request would be the same format as the URL if you were to view that page in your browser.
With what you provided I can't tell exactly what it should be, but I can give you an example. Assuming you access your index page by just "http://example.com/index" (or something similar), you would also call the function you want as something based on it's route. If the route was "update", then it would be something like "http://example.com/update".
Basically, don't use the folder path but use the web path instead.
Second, you don't do anything your success callback in your AJAX request. With AJAX, you send and receive info from the server, then you have to use JavaScript to update whatever you need updated manually. So, you'll need to have your update action return some information which you can then use to change the state of your second set of boxes.

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.

Categories

Resources