Reputation: 169
So I have this partial that has 2 variables: coursetitle
and routenumber
. I tried setting these variables from the view but I can't access these from the partial unless I make them global. I am looking for a way to pass on these variables from either the view or the controller but I am a bit lost as to why I can't set them from the view.
<div>
<h2> Correct!</h2>
<%= link_to "Next", "/courses/#{coursetitle}_q#{routenumber}",class:"btn btn-warning" %>
<%= render 'layouts/coursefooter' %>
<!-- fix for bootstrap navbar render bug -->
<script>
$( ".puller" ).addClass( "pull-left" );
</script>
I call this partial by :remote like this:
<%= link_to "Correct", correct_answer_courses_path,:remote => true,:class=>"btn btn-warning" %>
And my correct_answer.js looks like this:
$("#correct_answer").html("<%= j render partial: "beginnercourse/correct" %>");
Upvotes: 0
Views: 140
Reputation: 10009
I'm a little unclear as to your setup. If correct_answer.js
is rendered at the same time as your view, you can make coursetitle
and routenumber
instance variables and set them in the controller (@coursetitle
and @routenumber
). Then you'll be able to access them in both the view and partial.
You could also look into using the cells gem, which I believe was created to handle problems like this. I don't know if there's a baked in way to pass a local variable into a partial in Rails.
But it sounds like correct_answers.js
is not rendered at the same time as your view. In which case you can make sure that the correct_answer_courses_path
route includes the values for coursetitle
and routenumber
. e.g. in your routes.rb file
get '/course/:title/route/:number', to: 'controller#action', as: 'correct_answer_courses'
and you'd get the path with correct_answer_courses_path(coursetitle, routenumber)
. and then in the controller action that renders the partial get those values with params[:title]
and params[:number]
, set them to instance variable there, and then make use of them in the partial.
Upvotes: 0
Reputation: 109
In controller, you can define instance variables @coursetitle
and @routenumber
$("#correct_answer").html("<%= j render partial: "beginnercourse/correct", coursetitle: @coursetitle, routenumber: @routenumber %>");
Upvotes: 1