3

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)

2 Answers 2

1

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 %>
Sign up to request clarification or add additional context in comments.

6 Comments

Think this is the answer
thanks @mandeep, but I have tried following with your code, but result still doesn't appear. Actually, I have tried with this $("#result").html("<%= params[:type] %>"); and it's works, a params displaying in browser. I don't know why if I'm using variable @result or else to store params[:type] (e.g $("#result").html("<%= @result %>");, it doesn't work.
@itx you are using a variable to store params[:type] because it'll give access to it in your js.erb file. Do you get any error in browsers console when you tried my code?
No, I don't get any error on browser console also stack trace too.
@itx can you edit your question and post what you get in browsers console and stack trace
|
1

To further @mandeep's efforts, let me give you some ideas on debugging

To debug this application, there are 3 things to consider:

  1. Are your params hitting your controller?
  2. Is your data being parsed & your response rendered correctly?
  3. 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

2 Comments

thanks @rickpeck, I got a problem, the problem is write typo of method def checking instead def cheking. btw, I'm confuse to accept answer, I have accept answer's mandeep, and your answer is good explain. I have voted up for you. sorry about that. Thanks for answer rick :D
Yeah keep Mandeep's answer - I saw your comments and presumed you needed help. I didn't mean for this to be an actual "answer", more interesting information about the debugging process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.