【问题标题】:How to link_to index of another user?如何链接到另一个用户的索引?
【发布时间】:2015-06-11 01:16:41
【问题描述】:

在目标显示页面中:

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

但我不想被带到我自己的目标路径,我想被带到我正在查看的任何用户的目标显示页面的目标路径。

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

如果您需要进一步的解释或代码来帮助我,请告诉我:-]

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller


    【解决方案1】:

    如果您定义了目标用户(或有办法访问用户的 id),您可以执行以下操作:

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

    然后你可以修改你的index 方法来处理user_id 参数:

    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
    

    如果他们的 id 作为参数传入,这应该呈现特定用户的目标。希望这会有所帮助!

    【讨论】:

    • 非常感谢 Zoran,这很有意义!我尝试了link_to goals_path(user_id: @user.id) 以及其他一些变体,但这给了我:undefined method id' for nil:NilClass` 你有更好的选择吗?
    • @AnthonyGalli.com 你需要对用户有一个引用,即@user需要被赋予一个值,或者你需要使用一些其他的变量。
    • @Jon 它有一个参考。如果我执行 rails console Goal.find(1) 会显示一个 user_id 附加到它:user_id: 2,
    • @AnthonyGalli.com Jon 是对的 - 您的 show 操作没有用户定义。您可以尝试在此处定义它,或者将@some_user 替换为@goal.user_id。我已经更新了我的答案。希望有帮助!
    • 对不起@Zoran,这也是我尝试过的事情之一,但这只会让我回到与以前相同的 current_user 目标索引,但现在 url 显示 0.0.0.0:3000/goals.2 也许我需要更改配置文件?
    【解决方案2】:

    您也可以采用完全不同的路由,并在获取users/:user_id/goals 时更加REST-ful,并将其作为路由添加到您的routes.rb 中

       get "/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
    

    【讨论】:

    • 感谢您的帮助!我尝试了你的代码给了我错误:ActionController::UrlGenerationError in Goals#show No route matches {:action=&gt;"user_goals", :controller=&gt;"goals", :user_id=&gt;nil} missing required keys: [:user_id]
    • 嗯...你试过&lt;% link_to user_goals_path(@goal.user_id) ... %&gt;
    • 我把方法放在控制器和模型中。不知道在哪里。我还按照您的建议尝试了这条线的变体:&lt;% link_to user_goals_path(@goal_user)也许您可以找个时间聊天以快速敲定这一点:)
    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多