目标显示页面:
<%= link_to goals_path, class: "btn" do %>
<span class="glyphicon glyphicon-list"></span> Goals
<% end %>
但是我没有被带到我自己的goal_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
如果您需要进一步的说明或代码来帮助我,请告诉我们: - ]
答案 0 :(得分: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作为param传入,这应该呈现特定用户的目标。希望这有帮助!
答案 1 :(得分:0)
你也可以采取一个完全不同的路线,并且更加关注抓取routes.rb
,并将其作为路线添加到 get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
< / p>
<% 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
方法
def LetterChanges(str)
str.downcase!
str = str.split(//)
alphabet_lower = ["a".."z"]
i = 0
letter = 0
while i < str.length - 1
if alphabet_lower[letter] == str[i]
str[i] = alphabet_lower[letter + 1]
i += 1
letter = 0 ## Added this for the instance when letter > 0 after an 'else'
else
letter += 1
end
end
str = str.join("")
return str
end