Reputation: 1015
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
Upvotes: 0
Views: 55
Reputation: 2508
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.
Upvotes: 1