2015-06-11 59 views
2

在目標顯示頁面如何link_to其他用戶的索引?

<%= link_to goals_path, class: "btn" do %> 
<span class="glyphicon glyphicon-list"></span> Goals 
<% end %> 

但不是被帶到我自己goals_path我要採取哪個用戶的目標顯示頁面的,我看goals_path。

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 

請讓我知道如果您需要進一步解釋或代碼幫你幫我: - ]

回答

1

如果你的目標用戶定義(或有訪問用戶ID的方式),你可以這樣做:

<% link_to goals_path(user_id: @goal.user_id) do %> 
    <span class="glyphicon glyphicon-list"></span> Goals 
<% end %> 

然後喲ü可以修改您的index方法來處理user_id PARAM:如果他們的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 

這應該呈現特定用戶的目標。希望這可以幫助!

+0

非常感謝卓然,這讓我很有意義!我嘗試了'link_to goals_path(user_id:@ user.id)',其中有一些其他變體,但是這給了我:'nil:NilClass'的未定義方法'id'你有更好的選擇嗎? –

+0

@ AnthonyGalli.com您需要對用戶進行引用,即'@ user'需要給定一個值,或者您需要使用其他一些變量。 – Jon

+0

@Jon它有一個參考。如果我使用軌道控制檯Goal.find(1),則會顯示附加的user_id:'user_id:2,' –

0

你也可以只採取完全不同的路線,並多一點REST-FUL拼搶users/:user_id/goals,它在你的routes.rb

get "https://stackoverflow.com/users/:user_id/goals", to: "goals#user_goals", as: "user_goals" 

添加爲路線和更新按鈕有

<% link_to user_goals_path(@goal_user) ... %> 

然後添加goals#user_goals方法

def user_goals 
    @goals = Goal.find_by({user_id: params[:user_id]}) 
    render :index # or some other view 
end 
+0

感謝您的幫助!我嘗試你的代碼給我的錯誤:目標中的ActionController :: UrlGenerationError#show 沒有路由匹配{:action =>「user_goals」,:controller =>「goals」,:user_id => nil} :user_id]' –

+0

嗯......你試過了嗎?<%link_to user_goals_path(@ goal.user_id)...%>' –

+0

我把這個方法放在控制器和模型中。不知道在哪裏。我也嘗試過這一行的變體,正如你也建議的那樣:''%link_to user_goals_path(@goal_user)'也許你可以在某個時候聊天,以便快速地發佈這些內容:) –