ruby on rails - Only getting the index for the current user -


i have pretty basic app. i've managed install devise. idea have users have created challenges (1 many relationship). want logged in user able see challenges have created.

i order correct in thinking can pass current user id parameter challenges of current user follows (assuming view set correctly)

      <%= link_to challenges_path(user_id: current_user.id), class: 'expandable' %> 

challenges controller

def index     @challenges = challenge.all     render :layout => false   end 

if default behaviour want index of challenges, can change controller action directly, , no need modify link_to add user_id

challenges controller

def index     @challenges = challenge.where(user: current_user) end 

now if want change behavior if user_id param set, can keep link_to , modify controller way

def index     @challenges = params[:user_id] ? challenge.where(user: current_user) : challenge.all end 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -