rubysherpas/forem

trying to create a new topic within a given category but getting missing required keys error

UsmanJ opened this issue · 7 comments

I am trying to implement a link in the categories show page which will then allow a topic within that category to be created. I am trying to use the following code:

    = link_to t('forem.forum.forums'), forem.forums_path
    » #{link_to forem_emojify(@category), [forem, @category]}
    = link_to "Create Topic", forem.new_forum_topic_path
  = render @category

I am getting the following error:

No route matches {:action=>"new", :controller=>"forem/topics", :id=>"general"} missing required keys: [:forum_id]

How can I overcome this issue?

Any help will be much appreciated.

use the inspector in your view and see if the forum_id is present in the html. If it's not, there's something wrong with what you've written above.

I assume it's Rails? Have you tried using Rails' form helpers?

@Yorkshireman It is indeed rails. There is no forum_id present. I believe that I am missing something which comes after the forem.new_forum_topic_path. I'm just not sure what it is that I am missing.

What file is that? And have you got a failing test for this?

@Yorkshireman The file is 'show.html.haml' for the categories.

I have a failing test for it. It is displaying the following error:

     Failure/Error: = link_to "Create Topic", forem.new_forum_topic_path, [forem, @category]

     ActionView::Template::Error:
       No route matches {:action=>"new", :controller=>"forem/topics", :id=>"general"} missing required keys: [:forum_id]

I've forked it down and trying to run rails server but I'm getting Error: Command not recognized
Usage: rails COMMAND [ARGS]

Any advice?

@Yorkshireman That is odd.

➜  martial_nation_forem git:(master) ✗ rails s
=> Booting WEBrick
=> Rails 4.2.4 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-16 13:49:26] INFO  WEBrick 1.3.1
[2016-01-16 13:49:26] INFO  ruby 2.2.3 (2015-08-18) [x86_64-darwin14]
[2016-01-16 13:49:26] INFO  WEBrick::HTTPServer#start: pid=97352 port=3000

Have you migrated the db's?

I think I am a step closer. Now I have changed the code to:

.category
  %h2
    = link_to t('forem.forum.forums'), forem.forums_path
    » #{link_to forem_emojify(@category), [forem, @category]}
    = link_to "Create Topic", forem.new_forum_topic_path(forem, @topic)
  = render @category

But I'm getting the following error:

     ActiveRecord::RecordNotFound:
       ActiveRecord::RecordNotFound

I believe the error is occuring at the end of line 5.

I figured it out. I was trying to create a post within a category. The category needed a forum within it first. And then I had to create a post within that forum!

Thanks for your help @Yorkshireman!