1

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?

Mini John
  • 7,855
  • 9
  • 59
  • 108

3 Answers3

1

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

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
1

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 %>
1

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
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Cheers Mat, This gives me `undefined method `issue_path' for #<#:0x007fe4f20c2888>`, Did i miss something ? – Mini John May 30 '14 at 16:21
  • Does `shallow` make things easier or messier ? Should i go with shallow: true ? – Mini John May 30 '14 at 16:23
  • Try using this: `form_for @issue, url: script_issue_path(@issue.script, @issue)` But I don't think you should do it, I think that Rails is supposed to guess this path himself... – MrYoshiji May 30 '14 at 16:25
  • Same output :/ Do i have to include the script_id also ?? Script > Issue > Comment – Mini John May 30 '14 at 16:27
  • Does the error comes from the same line as the `form_for`? And yes as it is 2 nested resources, you need the ID of the 2 resources – MrYoshiji May 30 '14 at 16:29
  • Ok' so i think there is the problem, i don't specify anywhere the script_id .. (I restarted the Server and It worked, only that the comments are not created.. Form Works but nothing gets in) – Mini John May 30 '14 at 16:31
  • What are the params given to the create action of the IssuesController? – MrYoshiji May 30 '14 at 16:31
  • Hey yoshiji, up for a quick chat ? => http://chat.stackoverflow.com/rooms/54897/regex-craziness – Mini John Jun 03 '14 at 19:04