Two controllers with create using the same model

Hello, I got two controllers, both should create new subscriptions (the name of the model). Now the thing is how to create in case of the controller not named subscriptions_controller.rb. To avoid flooding the list I put both controllers and the view in the other controller (the one that fails) and the model itself to Pastie: http://pastie.org/899454 Long story short: why do I get „NoMethodError in StudentsController#create”, why does one controller tries to use the method from the other controller, and not his own? Thanks a lot for the reply. Peter

Any ideas? I still can't find out why this does not work, or how to solve. Or you guys prefer pasting code into the email instead of http://pastie.org/899454 ? Thanks a lot. And Happy Easter!

in your link I can see: beiratkozas_controller.rb subscriptions_controller.rb

but where is students_controller.rb (StudentsController) ?

Dose ruby asking you the same question?:slight_smile:

In addition, it happens because you using <% form_for(@student) do | f> %> helper if you look in HTML code (this helper appears to) you can find that request will be send to student controller...

Hello, Thanks a lot, and here is my students_controller.rb: http://pastie.org/900218 So I had one, but forgot to paste it to pastie. Peter

so now we can see exactly what rails told you - it is now create method in student controller. :slight_smile: and why you view requesting student controller i already told you. :slight_smile:

You got me wrong. I can't put the create method in the student controller. The question if, how could I use the create method from the BeiratkozasController class, while the model is NOT Beiratkozas, but Student. WSZP

Have a look at the docs for form_for, you will see it has a url parameter which will allow you to specify the controller and action that you wish to be performed on the submit.

Colin

Thanks a lot Colin, that was my problem.

From documentation I managed to make a working form_for:

<% form_for :student, @student, :url => { :action => "create" }, :html => { :method => :post, :id => "beiratkozok" } do |f| %>

This works, but I don't understand why do I need the :student in the front. What does it do?

Thanks a lot Colin, that was my problem.

From documentation I managed to make a working form_for:

<% form_for :student, @student, :url => { :action => "create" }, :html => { :method => :post, :id => "beiratkozok" } do |f| %>

This works, but I don't understand why do I need the :student in the front. What does it do?

It is in the docs, the :student says that the object being displayed and submitted is of type student, the @student is the variable containing the student. In fact since these are the same word you do not need both, either :student or @student will work I think, though using @student is more usual I think. Having both allows the class name and variable to be different.

I don't know why you have the html options they should not be needed.

I also don't understand why this is going to the beiratkozas controller, I would have expected it to go to the students controller. I would have expected you to put :url => { :controller => 'beiratkozas', :action => "create" } to send it to that controller. Check the html of the page (View, Page Source or similar in browser).

By the way it may be a good idea to put your comments inline with the previous posts as it is then easier to follow the thread.

Colin

Thanks a lot Colin,

First of all, I'm using Rails 2.3.5. and Ruby 1.8.7. The generated HTML looks like this: <form method="post" id="beiratkozok" action="/beiratkozas"> The option suggested by you: <% form_for :student, @student, :url => {:controller => 'beiratkozas', :action => "create" }, :html => { :method => :post, :id => "beiratkozok" } do |f| %> gives the very same result, so somehow the form_for assumes that controller is 'beiratkozas'. Maybe because the path is: Views/beiratkozas/ new.html.erb to the view that contains the form_for.

And works, since I have:

  map.connect 'beiratkozok', :controller => 'beiratkozas', :action => 'new'   map.connect 'hallgato/:id', :controller => 'beiratkozas', :action => 'show'   map.connect 'beiratkozas', :controller => 'beiratkozas', :action => 'create'

I know that map.resources :beiratkozas would be a more Rails like approach, but it's more human readable for Hungarians if I use meaningful Hungarian words.

The html is added since I need the ID for CSS, and I wanted a meaningful word as ID, since it makes CSS easier to read. On the other hand :method => :post was added since I saw something like that somewhere, and it made sense, since I want to use the POST verb in this case. Right?

Peter

P.S.: What do you mean putting my comments inline with the previous post? I'm simply hitting reply.

Thanks a lot Colin,

First of all, I'm using Rails 2.3.5. and Ruby 1.8.7. The generated HTML looks like this: <form method="post" id="beiratkozok" action="/beiratkozas"> The option suggested by you: <% form_for :student, @student, :url => {:controller => 'beiratkozas', :action => "create" }, :html => { :method => :post, :id => "beiratkozok" } do |f| %> gives the very same result, so somehow the form_for assumes that controller is 'beiratkozas'. Maybe because the path is: Views/beiratkozas/ new.html.erb to the view that contains the form_for.

Well obviously if it is a beiratkozas view it will default to sending it to that controller. I thought the whole point of the question was that you wanted to send it to a different controller. I see now that that you thought that the fact that it was a Student that it was manipulating would make it send it to that controller. That is not the case. There is no explicit link between a model and a controller as far as rails is concerned. Often a controller does manage a model of the same name but this is not necessary (as you are finding yourself). A controller can manipulate models of a different name, or several different model classes, or no model at all. Rails does not care.

And works, since I have:

map.connect 'beiratkozok', :controller => 'beiratkozas', :action => 'new' map.connect 'hallgato/:id', :controller => 'beiratkozas', :action => 'show' map.connect 'beiratkozas', :controller => 'beiratkozas', :action => 'create'

I know that map.resources :beiratkozas would be a more Rails like approach, but it's more human readable for Hungarians if I use meaningful Hungarian words.

The html is added since I need the ID for CSS, and I wanted a meaningful word as ID, since it makes CSS easier to read. On the other hand :method => :post was added since I saw something like that somewhere, and it made sense, since I want to use the POST verb in this case. Right?

The submit for a form_for will default to POST, but it does no harm to include it explicitly

Colin

[SOLVED!] Thanks a lot, now I do understand how this work. Your help was greatly appreciated.