2 Replies - 1213 Views - Last Post: 27 April 2014 - 06:32 PM Rate Topic: -----

#1 samarfikri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-April 14

Can someone please explain what this code does exactly?

Posted 27 April 2014 - 06:17 PM

class LikesController < ApplicationController

  def like

    @likeableid = params[:likable]
    @liker = params[:id]#cookies[:user_id]
    @type = params[:type]
    @currentAction = params[:currentaction]
    @currentontrol = params[:currentcontrol]

    @new = Like.new
    @new.likable_id = @likeableid
    @new.liker_id = @liker
    @new.likable_type = @type
    @new.save

    redirect_to :action=> @currentAction , :controller => @currentontrol
    return
  #<a href="../likes/like?currentcontrol=comment&currentaction=index&type=comment&likable=<%= comment.id%> >dasdsa</a>
  #localhost:3000/likes/like?currentcontrol=projects&currentaction=new&type=comment&likable=1&id=2
  end


  def showposts
    @lik = Like.find(:all).group_by(&:likable_id)
  end

end


This post has been edited by xclite: 27 April 2014 - 06:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone please explain what this code does exactly?

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,208
  • Joined: 12-May 09

Re: Can someone please explain what this code does exactly?

Posted 27 April 2014 - 06:24 PM

Please use code tags when posting.

Additionally, what is your specific question? Do you have any thoughts on how it does work? We'd prefer if you didn't post several topics with the goal of having somebody explain each line of code to you as you encounter new things.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Can someone please explain what this code does exactly?

Posted 27 April 2014 - 06:32 PM

Well the first part is simply taking in values from a page request (you are looking at a ruby on rails example here). It uses symbols to pull out data from the params array (data passed by the user's request) to set up a series of variables (the values preceded by the @ symbol). It uses these values to create a "Like" object, configure its parts and then save it. It then redirects the user to another action. What this code is essentially doing is saving a person's "Like" when they like something (clicking some like button). Look at the two lines that are commented out (has the # marks). These are examples of links that would trigger this controller to fire and save the like.

The showposts part is simply going to find all likes and group them by their like id and save that in the @lik variable. Probably to display later.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1