使用rails渲染页面

时间:2013-04-07 00:15:10

标签: ruby-on-rails ruby-on-rails-3 activerecord actioncontroller

我正试图在模特计划和学生之间建立“友谊”。我用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}?" %>

route.rb

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

已更新

关系控制器

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

rake routes

    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

1 个答案:

答案 0 :(得分: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}?" %>