I have a method in controller –
def undo_link
view_context.link_to("undo", revert_version_path(@page.versions.scoped.last), :method => :post)
end
But when i call it in views with "#{undo_link}" . I get the
undefined local variable or method error .
what am i doing wrong ?
Update : Here the revert method in the controller
okay but then this is the revert method i got in the controller
def revert
@version = Version.find(params[:id])
if @version.reify
@version.reify.save!
else
@version.item.destroy
end
link_name = params[:redo] == "true" ? "undo" : "redo"
link = view_context.link_to(link_name, revert_version_path(@version.next, :redo => !params[:redo]), :method => :post)
redirect_to :back, :notice => "Undid #{@version.event}. #{link}"
end
As Dave Newton said in the comments (+1), this belongs in a helper.
in
helpers/pages_helper.rb:Note that you will need to define
view_contextbefore this will work, either in the space indicated, passing it in as an argument, or by using an instance variable accessible to the view at the time the helper method is called.To call it in a view:
If you do this…
…you will get this:
which will not work.