ruby on rails - Authlogic Current User Question - hiding admin links -


I think I'm losing something using Authlogic gem w / rail. I have multiple users to set up the forum and every user can create posts and comments. On the display of a post or comment I would like to give to the user who has made the choice to edit or delete them.

I have been successfully hiding using the following code and if a user is logged or not, but it can not seem to figure out how these links are made only by the real user, Created ... Any users who are not logged in.

  & lt;% if current_user% & gt; & Lt;% = link_to 'edit', edit_question_path (question)%> | & Lt;% = link_to 'deleted', question, confirmation => 'Are you sure?' ,: method = & gt; :% & Gt; Remove & lt;% else% & gt; & Lt; P & gt; Nothing to see here & lt; / P & gt; & Lt;% end% & gt;  

Here is the def of the current controller which is located in the application controller, if I have to change something here.

Class ApplicationController & lt; ActionController :: Base Accessories: All #include all benefactor, all the time protect_from_forgery # See ActionController :: RequestForgery security # Description helper_method: current_user private def current_user_session return @current_user_session that defined? (@ Current_user_session) @current_user_session = UserSession.find end def Wartman_usr If @ is defined return @ (@ Chalu_usr) @ Srunt_usr = Chalu_yujr_seshn and amp; Amp; Try this:

itemprop = "text">

Code> Classroom Application Controller & lt; ActionController :: Base # Add your methods (like: current_user etc.) helper_method: current_user,: logged_in ?,: current_user_is_owner? Def init_data klass = controller_name.camelize.singularize.constantize #User param_key = controller_name.camelize.downcase.singularize.to_sym #: User obj = case (action_name.to_sym) when new ,: make klass.new (parameter [param_key]) When: Edit, Show, Delete: Clause.Find (Paramos [: ID]): When Update Tribulation. (Param [: ID]). Tap {| O | O.attributes = params [param_key]} and instance_variable_set ( "@ # {param_key}", obj) # set @line_item end def to ob_ Kren_sir returns TRUE Log_in? render_error_message return is if false end def require_owner obj = instance_variable_get ( "@ # {controller_name.singularize.camelize.underscore}") # line items current_user_is_owner @line_item true return (, new_user_session_url "You must be logged in to view this page ")? (Obj) render_error_message ("You must be # {controller_name.singularize.camelize} owner to use this page", root_url) Returns the false end DEF log? Return current_user! = Zero end DRF current_user_is_owner? (Obj) Login? And obj.respond_to? (: User_id) and (obj.send (: user_id) == current_user.id) end df render_error_message message, url response_to do | Format | Format.html To Flash [: Notice] = Message if request.xhr? Head: bad_request ,: error => Message and redirect_to url end format. Jason {Render: json = & gt; Message,: Status = & gt; : Unprocessable_entity} format.xml {Render: xml = & gt; Message,: Status = & gt; : Unprocessable_entity} End End

Now in your controller

  class post controller & lt; ApplicationController before_filter: require_user # all functions to log the user's need before_filter :, require_owner: init_data # action called @post a member variable based on before_filter, start making only = & gt; [Edit, update: deleted] # Edit, update, and deleted operations require ownership defaults, if @ Post alert and end and end  

view In code:

  & lt;% if current_user_is_owner? (Question)%> .. some more & lt;% end% & gt; Display.  

Comments