undefined method `post_comments_path' for #<#<Class:0xa3ddb8c>:0xb501ae4> - ruby-on-rails

I have a form which is part of a "comments" object, which is the child of a "post" object which in turn is the child of a "Category" object, I get the above error message with the following code (using simple_forms) Any advice?
= simple_form_for([#post, #post.comments.build], html: {class: 'form-horizontal'}) do |f|
= f.input :comment, label: "Your Reply", input_html: {class: "form-control"}
= f.submit
routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :categories do
resources :posts do
resources :comments
end
end
root 'categories#index'
end

You have defined your comments route under posts, which is nested under categories. With proper indentation, the error is easy to see:
Rails.application.routes.draw do
resources :categories do # Everything is nested under here
resources :posts do
resources :comments
end
end
end
So you have a categories_posts_comments_path.
If you run rake routes in the console you should see an output of all you existing routes. If you don't want this behaviour:
resources :posts do
resources :comments
end
resources :categories do # Everything is nested under here
resources :posts
end
But beware that this will duplicate a lot of routes, so you will want to use the only or except arguments to limit the number of routes generated.

Turns out I had to change the link to this:
= simple_form_for([#category, #post, #post.comments.build], html: {class: 'form-horizontal'}) do |f|
and changes the routes to:
Rails.application.routes.draw do
devise_for :users
resources :categories do
resources :posts
end
resources :posts do
resources :comments
end
root 'categories#index'
end

Related

undefined method `post_up_vote_path'

I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)

Rails one to many association

I have a one to many association between jobs and companies and it work fine now i want in the job show view to have a link to the company who has published the job for this in my job show view i put this code
<p>
<strong>company:</strong>
<%= link_to #job.company_name, company_path %>
</p>
but instead of going to http://localhost:3000/companies/company_id I’m redirecting to http://localhost:3000/companies/job_id where is the fault in my view
this is my routes
Ecole::Application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :jobs
resources :companies
resources :posts, only: [:show, :create]
devise_for :users
root "welcome#index"
get "profiles/show"
get '/profile/:id' => 'profiles#show', :as => :profile
get 'profiles' => 'profiles#index'
Replace
<%= link_to #job.company_name, company_path %>
With
<%= link_to #job.company_name, company_path(#job.company) %>
I think you should be passing the company id as a param for the company_path route. Try with company_path(:id=> company_id) with company_id being the targeted company id.

Using acts_as_commentable_with_threading

I'm trying to get this plugin to work. I've read other threads, but I dont seem to get it.
I'm trying to make my article model commentable. This if what i have done so far:
Done all the installation correctly.
class CommentsController < ApplicationController
def create
#article = Article.find(params[:id])
#user_who_commented = #current_user
#comment = Comment.build_from( #article, #user_who_commented.id, "Hey guys this is my comment!" )
end
end
In articles#show:
<%= form_for #comment do |f| %>
<%= f.text_area :text %>
<%= f.submit "Post Comment" %>
<% end %>
Routes:
devise_for :users
resources :dashboard
resources :users, :only => [:index,:show,:edit,:update]
resources :events
resources :januscript do
resources :quotes
end
resources :jmail, :only => [:index, :show]
resources :album
resources :video, :only => [:index, :show]
resources :photos
scope '/archive' do
resources :quotes, :only => [:index]
resources :articles, :only => [:index,:show]
end
resources :comments
I get this error message:
undefined method `model_name' for NilClass:Class
Make sure to add acts_as_commentable to your model that you want comments on. Not seeing any model code here.
Edit:
Did you add the nested resource in your routes for articles as well?
resources :articles do
resources :comments
end

Rails route not finding corresponding action

Inside app/views/participants/index.html.erb:
<%= form_tag bulk_add_participants_program_path do %>
<%= wrap_control_group do %>
<%= text_area_tag :bulk_add_participants, :size => "60x3" %>
<% end %>
<%= submit_tag "Import Participants and Users" %>
<% end %>
But notice that the controller and route pertain to the Program model (for good UI reasons). And I think that might be related to the problem. When I render that view I get this error message:
No route matches {:action=>"bulk_add_participants", :controller=>"programs"}
Which is weird because in app/controllers/programs_controller.rb:
def bulk_add_participants
puts "yay!" # because i am troubleshooting
end
And my config/Routes.rb is:
RepSurv::Application.routes.draw do
root to: 'programs#index'
devise_for :users, path_prefix: 'devise'
resources :users
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
get 'bulk_add_participants'
end
end
end
It's not finding the route because you have programs defined as a plural resource:
resources :programs do
When you do that and reference a member route like your bulk_add_participants, it expects a :program_id parameter in your case. (Try running rake routes, and you'll see a path like /programs/:program_id/bulk_add_participants.)
So your form_tag call should perhaps look like this:
<%= form_tag bulk_add_participants_program_path(#program) do %>

Form submission to different controller - Routing

I have this in my view:
<%= form_for #comment, :as => :post, :url => user_ticket_message_comments_path, :html => { :class => "add-comment", :id => "add-comment-" + #ticket.id.to_s } do |f| %>
<%= f.label :body, "Add comment" %>
<%= f.text_area :body %>
<%= f.submit "Add comment" %>
<% end %>
In my routes.rb:
resources :users do
resources :tickets do
resources :messages do
resources :comments
end
end
end
I get a routing error:
No route matches {:controller=>"comments"}
It looks like you are missing the user, ticket and message:
user_ticket_message_comments_path(#user, #ticket, #message)
You need those parameters, because the URL generated looks like this:
/users/:user_id/tickets/:ticket_id/messages/:message_id/comments
without the arguments, Rails doesn't know how to generate that URL.
Also, consider that the rule of thumb in Rails apps is that "Resources should never be nested more than 1 level deep."
This post by Jamis Buck suggests that instead of arbitrarily nesting resources, you only use one level of nesting like this:
resources :users do
resources :tickets
end
resources :tickets do
resources :messages
end
resources :messages do
resources :comments
end

Resources