I'm struggling to get this working, I've read a lot but couldn't find whats the problem here.
routes.rb
resources :scripts do
resources :reviews
resources :issues do
resources :comments
end
end
comments_migration
create_table :comments do |t|
t.integer :issue_id
t.integer :user_id
t.text :body
t.timestamps
end
controller action
def create
@issue = Issue.find(params[:issue_id])
@comment = current_user.comments.build(comment_params)
@comment.issue_id = @issue.id
if @comment.save
redirect_to @comment, notice: 'Comment was successfully created.'
else
render :new
end
end
def new
@issue = Issue.find(params[:issue_id])
@comment = current_user.comments.new
@comment.issue_id = @issue.id
end
Now in my Issues/Show
view i want to add the form for adding a comment:
<%= form_for [@issue, @comment] do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Routes:
POST /scripts/:script_id/issues/:issue_id/comments(.:format) comments#create
new_script_issue_comment GET /scripts/:script_id/issues/:issue_id/comments/new(.:format) comments#new
edit_script_issue_comment GET /scripts/:script_id/issues/:issue_id/comments/:id/edit(.:format) comments#edit
script_issue_comment GET /scripts/:script_id/issues/:issue_id/comments/:id(.:format) comments#show
PATCH /scripts/:script_id/issues/:issue_id/comments/:id(.:format) comments#update
PUT /scripts/:script_id/issues/:issue_id/comments/:id(.:format) comments#update
DELETE /scripts/:script_id/issues/:issue_id/comments/:id(.:format) comments#destroy
script_issues GET /scripts/:script_id/issues(.:format) issues#index
Which gives me First argument in form cannot contain nil or be empty
.
Although the request info shows:
{"action"=>"show", "controller"=>"issues", "script_id"=>"10", "id"=>"8"}
Do i have to include a :script_id
also to the comments?
What am i missing here?
You can use a simple form_for
to create the Comment:
form_for @issue.comments.build, url: script_issue_comments_path(params[:script_id], @issue) do |f|
f.text_area :body
f.submit "save"
end