Reputation: 7234
I have 2 controllers that render a single view and I need the view to change depending on my controller. What I have in mind is to have a JS var in my view that I can set depending on which controller was called.
To be a bit more thorough on my explanation, I need to get a string value set inside a var so I can use it like this (in a piece of JS on the application.js):
if (value_i_set_in_controller == "controller1") {
// do something
} else {
// do something else
}
Any hints? Until know I have been finding Rails pretty easy.
I need to explain a bit more.
I have some js in my application.js which renders some objet.
$('#calendar').fullCalendar({
...
// a future calendar might have many sources.
eventSources: [{
url: '/DYNAMIC_PATH',
color: 'yellow',
textColor: 'black',
ignoreTimezone: true
}],
I render this simply by having a calendar view with this :
<div id='calendar'></div>
I was thinking of doing something like this in the calendar view:
<div id='calendar'></div>
<script type="text/javascript">
var calendar_source = <% @dyn_path %>;
</script>
Evidently it does not work... with this I would be able to read calendar_source var and us it in my js to set the eventSources properly.
Upvotes: 0
Views: 84
Reputation: 10395
I think you're just missing quotes :
<script type="text/javascript">
var calendar_source = '<%= @dyn_path %>';
</script>
Anyway I need this kind of assignation often, it works perfectly :) If it still doesn't work, look at the generated code and the JS console (firebug, chrome debugger ...)
Upvotes: 1
Reputation: 7234
Don't know if this is a clean way to do but here it goes:
In my application.js I put
eventSources: [{
url: '/events/' + $('#calendar_event_path').data("value"),
color: 'yellow',
textColor: 'black',
ignoreTimezone: true
}],
Then in each controller is set a value like this:
@calendar_event_path = "company_events_index"
Then my view looks like this:
<div id='calendar'></div>
<div id='calendar_event_path' data-value='<%= @calendar_event_path %>'></div>
Upvotes: 0