Reputation: 2866
In the goals show page:
<%= link_to goals_path, class: "btn" do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
but instead of being taken to my own goals_path I want to be taken to the goals_path of whichever user's goal show page I'm looking at.
goals_controller
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@accomplished_goals = current_user.goals.accomplished.order("deadline")
@unaccomplished_goals = current_user.goals.unaccomplished.order("deadline")
end
end
def show
@goal = Goal.find(params[:id])
@commentable = @goal
@comments = @commentable.comments
@comment = Comment.new
@notable = @goal
@notes = @notable.notes
@note = Note.new
@correct_user = current_user.goals.find_by(id: params[:id])
end
def new
@goal = current_user.goals.build
end
def edit
end
def create
@goal = current_user.goals.build(goal_params)
if (params[:commit] == 'conceal')
@goal.conceal = true
@goal.save
redirect_to @goal, notice: 'Goal was successfully created'
elsif
@goal.save
track_activity @goal
redirect_to @goal, notice: 'Goal was successfully created'
else
flash.now[:danger] = 'Required Field: "Enter Goal"'
render 'new'
end
end
def update
if @goal.update(goal_params)
redirect_to goals_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
@goal.destroy
redirect_to goals_url
end
def like
@goal = Goal.find(params[:id])
@goal_like = current_user.goal_likes.build(goal: @goal)
if @goal_like.save
@goal.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_goal
@goal = Goal.find(params[:id])
end
def correct_user
@goal = current_user.goals.find_by(id: params[:id])
redirect_to root_url, notice: "Not authorized to edit this goal" if @goal.nil?
end
def goal_params
params.require(:goal).permit(:name, :like, :deadline, :accomplished, :tag_list, :comment, :private_submit)
end
end
Please let me know if you need further explanation or code to help you help me :-]
Upvotes: 2
Views: 82
Reputation: 51
You could also just take a completely different route, and be a little more REST-ful about grabbing users/:user_id/goals
, and add it as a route in your routes.rb
get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
and update your button to have
<% link_to user_goals_path(@goal_user) ... %>
and then add the goals#user_goals
method
def user_goals
@goals = Goal.find_by({user_id: params[:user_id]})
render :index # or some other view
end
Upvotes: 0
Reputation: 4226
If you have a target user defined (or have a way to access a user's id), you can do something like:
<% link_to goals_path(user_id: @goal.user_id) do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
Then you can modify your index
method to handle a user_id
param:
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
elsif params[:user_id]
@goals = User.find(params[:user_id]).goals
else
@accomplished_goals = current_user.goals.accomplished.order("deadline")
@unaccomplished_goals = current_user.goals.unaccomplished.order("deadline")
end
end
This should render a particular user's goals if their id is passed in as a param. Hope this helps!
Upvotes: 1