0

I have an array @fields which has text and an id, asset_id. To pass local variables from controller to view in render we use

render(:template => "assets/invalid", :locals => {:asset_id => params[:id], :fields => @fields})

The view is

<div id="panel">
  <script>
    alert('Invalid values for ')
    window.location = "../assets/" 
  </script>
</div>

This should generate a popup box. However, I want the popup box to redirect to "../assets/asset_id" and also display 'Invalid values for + fields'

The following doesn't work,

<div id="panel">
  <script>
    var fields = fields
    var asset_id = asset_id 
    alert('Invalid values for ' + fields )
    window.location = "../assets/" + asset_id 
  </script>
</div>
4
  • When you do rake routes do you see /assets/:id ? Commented Oct 15, 2012 at 16:51
  • Yes, I do see it. The alertbox redirects to ../assets/. I want it to redirect to ../assets/asset_id. Commented Oct 15, 2012 at 16:53
  • Post how you are getting the asset_id and how you want to send it. Since its html.erb you can just grab it. Commented Oct 15, 2012 at 16:56
  • The assets_id is from the controller. I want to send it to the script in the view to create the location of that asset, i.e. redirect to that ID page. The :local within render should send it to the view, shouldn't it? The JS has to create that link for window.location to redirect to. Commented Oct 15, 2012 at 17:02

3 Answers 3

3

Could it be as simple as this?

<div id="panel">
  <script>
    var fields = <%= fields.to_json %>
    var asset_id = <%= asset_id.to_json %>
    alert('Invalid values for ' + fields )
    window.location = "../assets/" + asset_id 
  </script>
</div>

UPDATE And by the way, why do you need to pass fields or params as a local variable, why not to use @fields in your view?

Sign up to request clarification or add additional context in comments.

3 Comments

The .to_json method isn't working. The view (alert box) doesn't show up. If I kept it as just fields as @Viren said, the alert box says 'Invalid values for undefined'.
Check page source -- what it's being converted to
fields is an array of strings and asset_id is a string
1

An approach I have used in a couple apps where it's necessary for the client code to use server data is to build a hash and render it into a json literal on the page and have all my JS reference that object. It's the same approach as mentioned above but it's a bit cleaner because you don't have to mix a lot of server tags into your JS code. Easier to read and maintain.

Comments

0

What wrong with this

<div id="panel">
  <script>
    var fields = fields
    var asset_id = <%= asset_id %> 
    alert('Invalid values for ' + fields )
    window.location = "../assets/" + asset_id 
  </script>
</div>

Now you can pass data to html for advance thing you can use something like gon check out gon gem it might help

2 Comments

The to_s of an array is not consistently parseable as JS across Ruby versions. Though the inspect output of an array mostly is, it's safer to go with to_json.
@Phrogz Point very well Expected

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.