0

I put all the code in this gist because I can't get the formatting to work.

https://gist.github.com/anonymous/72e66308c236a0277943

What I am trying to do is to have a form for the prof_comments model on the Professors page.

Whenever I try and submit the form I currently have for the prof_comments model, it tries to post to the current professors show page (/professors/1)

I've been trying to follow the following StackOverflow posts, but no luck yet.

Rails: Show form from different model in a view

Possible to add a form into another models view in rails

Routes.rb

Rails.application.routes.draw do


  root :to => "welcome#index"
  devise_for :users
  resources :users

  resources :professors
  resources :prof_comments
  resources :classes
  resources :class_comments

end
Community
  • 1
  • 1
Zeratas
  • 1,005
  • 3
  • 17
  • 36
  • so you want the prof_comments to appear under the professor's `show page`? Like a post and a nested comments? please explain more. also, post your `routes.rb` – Wally Ali May 19 '14 at 01:15
  • Yes, I want to have a form to add a new prof_comment for that professor under its show page. I just added routes.rb. – Zeratas May 19 '14 at 01:17

2 Answers2

1

You have to use

form_for @prof_comment

with @ not :

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • I think I understand. The @prof_comment means I'm referencing that prof_comment instance I defined in the controller under the show route? – Zeratas May 19 '14 at 01:25
  • Good explanation here of the difference http://stackoverflow.com/questions/2006329/ruby-on-rails-symbol-as-argument-in-form-for – Eyeslandic May 19 '14 at 01:26
  • Ok. So it looks like my next problem is going to be to send the professor id to the prof_comments controller. This is gonna be fun. – Zeratas May 19 '14 at 01:34
  • 1
    You can use `hidden_field_tag 'professor_id', 1` then you get the professor_id in `params[:professor_id]` – Eyeslandic May 19 '14 at 01:48
1

I'm not sure about the approach you were trying but this will do exactly what you are trying to do: have the professor's comments appear under it's show page.

You can nest prof_comments under professors

your routs will look like this:

resources :professors do 
   resources :prof_comments, shallow: true
end

prof_comments controller:

 def create 
    @professor = Professor.find(params[:id]) #this pulls specific professor by :id
    @prof_comment = @professor.prof_comments.create(prof_comment_params)
    redirect_to professor_path(@professor) # this will rout you to professor's show page once the comment is created. 
    end
 end

in app/views/professors/show.html

 h2>Add a comment:</h2>
  <%= form_for([@professor, @professor.prof_comments.build]) do |f| %>

   <%= f.label :commenter %><br> #commenter with the actual attribute_name
   <%= f.text_field :commenter %> #commenter with the actual attribute_name

   <%= f.label :body %><br> #body should be replaced with your actual attribute_name
   <%= f.text_area :body %> #body should be replaced with your actual attribute_name

   <%= f.submit %>

<% end %>

these comments will appear under the professor show view. the comments are nested under it. Treat the professor's controller as usual. you'll be using to create the comments using prof_comments controller.

Wally Ali
  • 2,500
  • 1
  • 13
  • 20
  • I'll have to try both out. I think I am going to accept yours now since it looks like it is more fleshed out. IT also looks like it concentrates more on linking it all together. Thanks! – Zeratas May 19 '14 at 02:47
  • 1
    btw, you can move the comment form to a partial and render it to avoid clutter. and by the way, this approach is from the [rails guides tutorial](http://guides.rubyonrails.org/getting_started.html). it will walk you through it – Wally Ali May 19 '14 at 03:15