0

我正在尝试在模型程序和学生之间建立“友谊”。我用 has_many 做:通过模型关系。在关系控制器中,我创建了动作,它应该在友谊发生后重定向另一个页面。

因此,在学生展示视图中,我有 link_to 请求友谊。我以为我可以进入学生展示页面,但是当我尝试进入学生展示视图到另一个页面时,它会将我从学生索引重定向。所以我实际上无法进入学生节目,它只是将我重定向到其他页面。

为什么会这样?单击“请求友谊”后,我希望从学生显示视图重定向到其他模型页面。

学生 show.html.erb

 <%= link_to "Request friendship with #{@student.name}",
 { :controller => "relationship", :action => "create",
 :id => @student.name },
 :confirm => "Send friend request to #{@student.name}?" %>

路由.rb

 match "/students/:id" => "relationships#create"

更新

关系控制器

def create
  Relationship.request(@prog, @student)
  flash[:notice] = "Friend request sent."
  redirect_to prog_url
end

耙路线

    relationships_create GET    /relationships/create(.:format)   relationships#create
    relationships_destroy GET    /relationships/destroy(.:format)  relationships#destroy
                         /students/:id(.:format)           relationships#create
    relationships GET    /relationships(.:format)          relationships#index
                  POST   /relationships(.:format)          relationships#create
 new_relationship GET    /relationships/new(.:format)      relationships#new
edit_relationship GET    /relationships/:id/edit(.:format) relationships#edit
     relationship GET    /relationships/:id(.:format)      relationships#show
                  PUT    /relationships/:id(.:format)      relationships#update
                  DELETE /relationships/:id(.:format)      relationships#destroy
         students GET    /students(.:format)               students#index
                  POST   /students(.:format)               students#create
      new_student GET    /students/new(.:format)           students#new
     edit_student GET    /students/:id/edit(.:format)      students#edit
          student GET    /students/:id(.:format)           students#show
                  PUT    /students/:id(.:format)           students#update
                  DELETE /students/:id(.:format)           students#destroy
            progs GET    /progs(.:format)                  progs#index
                  POST   /progs(.:format)                  progs#create
         new_prog GET    /progs/new(.:format)              progs#new
        edit_prog GET    /progs/:id/edit(.:format)         progs#edit
             prog GET    /progs/:id(.:format)              progs#show
                  PUT    /progs/:id(.:format)              progs#update
                  DELETE /progs/:id(.:format)              progs#destroy
4

1 回答 1

1

不确定我完全理解@prog 应该是什么,但这是要走的路

def create
  @student = Student.find_by_id(params[:id])
  @prog = Programme.find_by_id(params[:prog]) #You need to fill this in
  Relationship.request(@prog, @student)
  flash[:notice] = "Friend request sent."
  redirect_to @student # Or @prog, or whatever you'd like
end

您可能还需要将 prog.id 传递给控制器​​,例如:

match "/students/:id/:prog" => "relationships#create"

并添加

<%= link_to "Request friendship with #{@student.name}",
 { :controller => "relationship", :action => "create",
:id => @student.name, :prog => @student.current_programme },
# I don't know how you differ programmes, you'd have to work this one out  
:confirm => "Send friend request to #{@student.name}?" %>
于 2013-04-07T09:05:07.190 回答