我在 UsersController 中写了一个“跟随”方法
def start_following
@user = current_user
@user_to_follow = User.find(params[:id])
unless @user_to_follow == @user
@follow_link = @user.follow_link.create(:follow_you_id => @user_to_follow.id, :user_id => @user.id)
@user.save
flash[:start_following] = "You started following" + @user_to_follow.name
else
flash[:cant_follow] = "You cannot follow yourself"
end
end
很简单。在视图中,我有
<%= link_to 'Follow', follow_user_path(@user) %>
在路线中,
resources :users do
member do
get 'follow' => "users#start_following", :as => 'follow'
当我点击链接时,它抱怨:Missing template users/start_following
那么,如何让它在操作后停留在同一页面上?我想留在的视图页面是 Show view of the user is to be follow。例如:用户/{user_id}。只是重定向不是解决方案吗?我认为添加redirect_to {somewhere}
会消除错误,但事实并非如此。