Ruby on rails 如何将_链接到其他用户的索引?
在目标显示页面中:Ruby on rails 如何将_链接到其他用户的索引?,ruby-on-rails,ruby,model-view-controller,Ruby On Rails,Ruby,Model View Controller,在目标显示页面中: <%= link_to goals_path, class: "btn" do %> <span class="glyphicon glyphicon-list"></span> Goals <% end %> 目标 但是我不想被带到我自己的goals\u路径,而是想被带到我正在查看的任何用户的goals show页面的goals\u路径 目标\u控制器 class GoalsController < Applic
<%= link_to goals_path, class: "btn" do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
目标
但是我不想被带到我自己的goals\u路径,而是想被带到我正在查看的任何用户的goals show页面的goals\u路径
目标\u控制器
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
class GoalsController
如果您需要进一步的解释或代码来帮助我,请告诉我:-]如果您定义了目标用户(或有办法访问用户id),您可以执行以下操作:
<% link_to goals_path(user_id: @goal.user_id) do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
如果将特定用户的id作为参数传入,则应呈现该用户的目标。希望这有帮助 你也可以选择一条完全不同的路线,在抓取
用户/:user_id/goals
时稍加休息,然后将其作为路线添加到你的路线中。rb
get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
并更新您的按钮,使
<% link_to user_goals_path(@goal_user) ... %>
非常感谢Zoran,这很有道理!我尝试了
link\u to goals\u path(user\u id:@user.id)
和其他一些变体,但这给了我:undefined method
id'for nil:NilClass`你有更好的选择吗?@AnthonyGalli.com你需要有一个对用户的引用,也就是说,@user
需要给定一个值,或者您需要使用其他变量。@Jon它有一个引用。如果我执行rails console Goal.find(1),则会显示附加到它的用户id:用户id:2,
@AnthonyGalli.com Jon的右侧-您的显示
操作没有用户定义。您可以尝试在那里定义它,或者用@goal.user\u id
替换@some\u user
。我已经更新了我的答案。希望有帮助!抱歉@Zoran这也是我尝试过的事情之一,但这只是让我回到当前用户的目标索引,与以前一样,但现在url显示我可能需要更改配置文件?谢谢帮助!我尝试了您的代码,但出现了错误:ActionController::UrlGenerationError in Goals#show No route matches{:action=>“user\u Goals”,:controller=>“Goals”,:user\u id=>nil}缺少必需的键:[:user\u id]
Hmm。。。你试过了吗?
我把方法放在控制器和模型中。不知道在哪里。我还尝试了这一行的变体,正如您所建议的:
<% link_to user_goals_path(@goal_user) ... %>
def user_goals
@goals = Goal.find_by({user_id: params[:user_id]})
render :index # or some other view
end