0

I'm still new to Ruby on Rails and I am stucking at one Point.

My Database contains a Teacher and a Course model. They are associated with a has_and_belongs_to_many Association (at the moment it shouldn't be changed).

Course Detail View:

<p> Teacher:
    <%= form_tag url_for(controller: 'courses', action: 'add_teacher') ,method: 'put' do%>
    <%= select_tag :teacher, options_for_select(Teacher.all.collect {|t| [t._name, t.id]})%>
    <%= hidden_field_tag(:course, value = @course.id) %>
    <%= submit_tag 'hinzufügen' %>
    <%end %>
<% end %></p>

Course_Controller:

def add_teacher
    @course = Course.find(params[:course])
    @teacher = Teacher.find(params[:teacher]) unless params[:teacher].nil?
    @course.teachers << @teacher
    redirect_to action: 'show'
end


def update
    redirect_to action: 'add_teacher', course: params[:course], teacher: params[:teacher]
end

That was a part of my code. I don't really know if there is a routing problem or something else. I just don't get the point why the function update is called before the function add_teacher is called. First I thought maybe rails realized that I would like to change a existing record, so the function "update" is called when the change (@course.teachers << @teacher) "is happening" . But he first calls the update function and it doesn't matter if I redirect to add_teacher or not, he would call the function anyway! why?

Now i'm using the update function to redirect to add_teacher with the correct params. May someone has a better solution for me or an answer why the function 'update' is called automatically and how to set the routes correctly?

Thanks a lot for help and sorry for my bad english!

Bye

update: Ok I was sure he redirects anyway, but I commented it out and now he isn't doing anything.

My Routes look like this:

get 'courses/add_teacher'
get 'courses/update'
resources :students, :forms, :votes, :courses, :years, :teachers

and a lot more like this

homior
  • 161
  • 1
  • 12
  • What do your routes look like – Eyeslandic Apr 26 '17 at 19:22
  • To conform to the REST model you should have `resources :courses` and inside that, `resources :teachers` so you can associate teachers with courses. – tadman Apr 26 '17 at 19:30
  • @tadman ok, should i rather map the function to a route like this: `post '/:courses/:teachers'`? I'm new to routing as well^^ – homior Apr 26 '17 at 19:41
  • Whenever possible let `resources` do the work for you. The [official Routing guide](http://guides.rubyonrails.org/routing.html) has many examples to work from. Avoid using `get` and `post` routes coded by hand unless there's no other way to get the results you want. If `/courses/:course_id/teachers` is an acceptable URL then there's no reason to override. – tadman Apr 26 '17 at 19:52

1 Answers1

0

Try changing your routes from...

get 'courses/add_teacher'
get 'courses/update'
resources :students, :forms, :votes, :courses, :years, :teachers

to...

put 'courses/:id/add_teacher'
resources :students, :forms, :votes, :courses, :years, :teachers

You were using GET which is used to display a page or a form. The action PUT (which you're using in your form) is the action used to update an object.

You don't need the update route you had because it's already defined when you called

resources :courses

(which is a PUT action).

My guess is that since you used PUT in your form, Rails bypassed the add_teacher method and took the first PUT action for courses which would be update.

You might what to think about nesting your resources in your routes. I think that would be easier to work with.

Hopefully this helps! :) You can read more here... http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions