I’m trying to pass a model attribute to a view, after successfully setting it to a new value from inside an action in my controller. But this variable is always nil by the time it gets to the view, so I can’t use it to conditionally display stuff. I should add that this attribute is not a field in the database. What am I missing/doing wrong?
Here is the code in my model:
attr_accessor :mode #getter def mode @mode end #setter def mode=(val) @mode = val end
…in the controller:
@report.mode = 't' redirect_to edit_report_path(@report)
…and in my view:
<%= build_report(@report.mode) %>
…but this helper method never gets the variable I just set in the controller. It is nil. What gives? Clearly I’m missing something basic here because this seems like it should be straightforward. Any insight would be greatly appreciated. Thanks.
edit_report_pathgenerates a URL with the ID of@reportin it.redirect_toessentially creates a whole new request, and goes to that URL. When it gets toedit, all it has is the ID. Usually that’s fine – it looks up the object and keeps going, but of course it’s not going to have the non-db field you set.There are a couple ways to fix this. You can use :render instead to get to the edit page – then @report will have the field set.
Or, you can make
modea database field.