最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does js.erb work - Stack Overflow

programmeradmin1浏览0评论

Lately i have run into a few applications that are using js.erb and i am not really sure how to use it ...here is the code below. Can someone help me understand how this works?

in the routes.rb file

map.resources :player_emails

my controller player_emails_controller.rb in the create action

def create
 @player_email = PlayerEmail.create(params[:player_email])
 if @player_email.save
  @response_txt = "The player has been emailed."
  PlayerEmailsMailer.deliver_pattern_email(@something, @player_email, request.host_with_port)
  @error = false
 else
  @error = true
  @response_txt = "Please make sure you entered your name and a valid email address."
 end
end

then i have the file player_emails/create.js.erb

$('#player_email_ind').hide();
$('#player_email_submit').show();
$('#player_response_msg').html("<%= escape_javascript @response_txt %>").fadeIn();
<% unless @error %>
$('#player_email_form')[0].reset();
<% end %>

i know what the jquery is going but i dont know how this is doing the ajax call. Does it just automatically do an ajax call when there is a js.erb...can someone explain the way this works and why i dont need a respond_to in the controller action telling it this is format.js

Lately i have run into a few applications that are using js.erb and i am not really sure how to use it ...here is the code below. Can someone help me understand how this works?

in the routes.rb file

map.resources :player_emails

my controller player_emails_controller.rb in the create action

def create
 @player_email = PlayerEmail.create(params[:player_email])
 if @player_email.save
  @response_txt = "The player has been emailed."
  PlayerEmailsMailer.deliver_pattern_email(@something, @player_email, request.host_with_port)
  @error = false
 else
  @error = true
  @response_txt = "Please make sure you entered your name and a valid email address."
 end
end

then i have the file player_emails/create.js.erb

$('#player_email_ind').hide();
$('#player_email_submit').show();
$('#player_response_msg').html("<%= escape_javascript @response_txt %>").fadeIn();
<% unless @error %>
$('#player_email_form')[0].reset();
<% end %>

i know what the jquery is going but i dont know how this is doing the ajax call. Does it just automatically do an ajax call when there is a js.erb...can someone explain the way this works and why i dont need a respond_to in the controller action telling it this is format.js

Share Improve this question edited Mar 21, 2016 at 19:20 Matthias 1,9532 gold badges20 silver badges37 bronze badges asked Jan 5, 2011 at 17:01 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 1
  • 1 i am just curious if there is a tutorial for writing .js.erb out there ... – max pleaner Commented Mar 30, 2014 at 23:29
Add a comment  | 

3 Answers 3

Reset to default 7

If a js (ajax) request is made it will respond by rendering the js.erb file and viceversa.

This is the default behaviour that is being performed:

  respond_to do |format|
    format.js{
      render :template => 'create.js.erb'
    }
    format.html{
      render :template => 'create.html.erb'
    }
  end

When the form is submitted, it does a POST to /player_emails. The resource declaration in routes.rb ensures the request is handled by PlayerEmailsController#create.

The controller is responsible for handling each format it receives. In the case of an AJAX call, the format is 'js', and is set by explicitly adding the format string to the end of the URL (/player_emails.js) or (more likely) by deducing the format from the request header.

In your case, the create action does not expect anything other than AJAX, so it takes a shortcut and omits the respond_to and format blocks. The controller has already figured out that the format is 'js', so when create is complete it takes the default action of rendering the appropriate template for the format (create.js.erb).

Does your form submit button have a :remote => true on it? If so, there might be some JavaScript in rails.js or application.js that automatically submits via AJAX. Bottom line is, there has to be some JavaScript somewhere that is making an AJAX call and asking for a js or JSON response, otherwise it would be an html request.

As for why you don't need a respond_to block, I'm not entirely sure. Maybe since the call is always being made by AJAX and there is a js.erb template available, it just does its thing without complaining. Is there an html.erb template at all? If not, try doing a regular form submit and see if it complains.

发布评论

评论列表(0)

  1. 暂无评论