I have controller like this
class HomesController < ApplicationController
def index
end
def cheking
@result = params[:type]
respond_to do |format|
format.js
end
end
end
And form on index.html.erb
<%= form_tag(checking_path, :remote => true, :format => :js, :method => :get) do %>
<%= select_tag "type", options_for_select([ "admin", "customer" ], "admin") %>
<%= submit_tag "check now" %>
<% end %>
<div id="result"></div>
checking.js.erb
file
$("#result").html("<%= escape_javascript(render("result")) %>");
I want to put @result
in render file _result.html.erb
<%= @result %>
but @result
does not appear on browser when submit form with admin
type.
log :
Started GET "/checking?utf8=%E2%9C%93&type=admin&commit=check+now" for 127.0.0.1 at 2014-09-14 01:26:13 +0400
Processing by HomesController#checking as JS
Parameters: {"utf8"=>"✓", "type"=>"admin", "commit"=>"check now"}
Rendered homes/_result.html.erb (0.4ms)
Rendered homes/checking.js.erb (1.1ms)
Completed 200 OK in 5.0ms (Views: 4.8ms | ActiveRecord: 0.0ms)
In your checking.js.erb you need to pass your variable so that your partial could access it . You can do:
$("#result").html("<%=j render partial: 'result', locals: {result: @result} %>");
and in your partial you need to use result
#_result.html.erb
<%= result %>
To further @mandeep
's efforts, let me give you some ideas on debugging
To debug this application, there are 3 things to consider:
- Are your params hitting your controller?
- Is your data being parsed & your response rendered correctly?
- Is your response firing as required?
Debug
To help you debug, you've provided us with some great information:
Started GET "/checking?utf8=%E2%9C%93&type=admin&commit=check+now"
This is very good as it means that your :type
param should be set (which would be the primary concern for an error like this). You can test this further by using the following:
#app/controllers/homes_controller.rb
class HomesController < ApplicationController
def checking
Rails.logger.info params[:type]
end
end
Presuming your data is hitting your controller, the next thing you want to do is to ensure it's being parsed & your response is being rendered correctly. To do this, you need to check your JS is actually being called, and whether it's firing:
#app/views/homes/checking.js.erb
alert("Firing");
$("#result").html("<%=j render partial: 'result', locals: { render: @render } %>");
#app/views/homes/_render.html.erb
<%= render %>
The above should fire an alert when you send the request to your controller. This will give you the ability to check whether the JS is actually firing, to which you'll be able to see where the problem may lie.
Finally, you'll then want to consider the response you're calling. Is this structured correctly? It might be the case that your syntax is incorrect, thus preventing you from being able to call the functionality you require. The code above should work for you in this regard
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.