Mini John
Mini John

Reputation: 7941

Deeply Nested resources Form

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?

Upvotes: 1

Views: 383

Answers (3)

Farrukh Abdulkadyrov
Farrukh Abdulkadyrov

Reputation: 717

It is happening because a parameter in form_for is nil. You should initialize it in your show action. You do not need script_id

class IssuesController < ApplicationController
    def show
     ..
     @comment = @issue.comments.build
     ..
end

Fix for undefined path error. You will need to modify form_for slightly.

<% @form_for @comment, url: script_issue_comments_path(@issue.script_id, @issue) do |f| %>
...
<% end %>

Upvotes: 1

MrYoshiji
MrYoshiji

Reputation: 54882

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

Upvotes: 1

BroiSatse
BroiSatse

Reputation: 44675

You are missing that form is being build upon a new action, not on create. You need to declare those variables there as well.

Upvotes: 1

Related Questions