0

I'm trying to make "friendship" between model Programme and Student. I do it with has_many :through model Relationship. In Relationship controller I have create action, which should redirect another page after the friendship occurs.

So, in Student show view I have link_to request friendship. I thought I will be able to enter Student show page, but instead it redirects me from Student index when I try to enter Student show view to another page. So I actually can't enter Student show, it just redirects me to other page.

Why is that happening? I want have redirect from Student show view after I click on "request friendship" to other model page.

Student 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"

UPDATED

Relationship controller

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 Answers1

1

Not sure I understand completely what @prog should be, but this is the way to go

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

You would probably need to pass the prog.id as well to the controller something like:

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

and add

<%= 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}?" %>
gmaliar
  • 5,294
  • 1
  • 28
  • 36