I'm getting the following error when I run my Rails app, "uninitialized constant Blog::PostsController"
routes.rb
Rails.application.routes.draw do
devise_for :authors
root to: 'blog/posts#index'
namespace :author do
resources :posts
end
scope module: 'blog' do
get 'about' => 'pages#about', as: :about
get 'contact' => 'pages#contact', as: :contact
get 'posts' => 'posts#index', as: :posts
get 'posts/:id' => 'posts#show', as: :post
end
end
posts.controller.rb
module Blog
class PostsController < BlogController
def index
#post = Post.most_recent
end
def show
#post = Post.friendly.find(params[:id])
end
private
def set_post
end
end
end
The directory path is as follows:
app/controller/blog/posts.controller.rb
I guess the problem is in Naming convention.
Here is example of naming convention:
Controller Naming Convention
Class: PostsController
File: /app/controllers/posts_controller.rb
For more detail on naming convention please check: http://www.ganeshkunwar.com.np/2018/01/02/naming-convention-rails/
Also inherit posts controller from application_controller not from blog controller.
Related
How to add custom URLs like localhost:3000/one_hour/page/2 instead of localhost:3000/one_hour?page=2
to get '/one_hour', to: 'feed_entries#one_hour'
I use Rails 4, Kaminari and mongoid
routes.rb
Rails.application.routes.draw do
concern :paginatable do
get '(page/:page)', :action => :index, :on => :collection, :as => ''
end
resources :feed_entries, path: 'news', :concerns => :paginatable
get '/one_hour', to: 'feed_entries#one_hour'
end
feed_entries_controller.rb
class FeedEntriesController < ApplicationController
one_hour
#feed_entries = FeedEntry.includes(:source).one_hour.page(params[:page])
end
end
I found a solution:
in routes.rb
get 'one_hour/(page/:page)', controller: 'feed_entries', action: 'one_hour', to: 'feed_entries#one_hour', as: :one_hour
I've got a problem with my vote methods for comments. Im a begginer at RoR and waiting for your suggestions.
The error I get is:
ActionController::RoutingError at /posts/51f7d1279fefa5405a000003 No
route matches {:controller=>"comments", :action=>"vote(1)",
:class=>"post__button--edit"}
My code:
comment.rb
class Comment
include Mongoid::Document
field :name, type: String
field :email, type: String
field :body, type: String
field :up_vote, type: Integer, default: "0"
field :down_vote, type: Integer, default: "0"
belongs_to :post
validates_presence_of :name, :email, :body
def self.add_up_vote
self.increment(:up_vote, 1)
end
def self.add_down_vote
self.decrement(:down_vote, 1)
end
end
comment_controller.rb
.
.
.
def vote(a)
#comment = Comment.find(params[:comment_id])
#post = Post.find(params[:post_id])
if a == 1
comment.add_up_vote
redirect_to #post
elsif a == -1
comment.add_down_vote
redirect_to #post
else
redirect_to #post
end
end
routes.rb
Easyblog::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users
resources :posts do
resources :comments
member do
post :mark_archived
end
end
end
Im waiting for your help :)
What is a here? I guess it's vote direction
You have to remove argument a from vote action and pass direction via params for link path.
Fox example:
vote_comment_path(#comment.id, dir: 1) # or dir: -1
More than that there is no route for vote action. You can describe it like so:
resources :comments do
put :vote, as: :member
end
upd I would recommend you to read following guide http://guides.rubyonrails.org/routing.html
action in your path is not valid. You link should looks like
= link_to 'Yes', vote_comment_path(comment, dir: 1), method: :put
vote_comment_path can be different you could check it by rake routes command:
$ rake routes
Try changing like this
Easyblog::Application.routes.draw do
resources :posts do
resources :comments do
match :vote
end
member do
post :mark_archived
end
end
end
you could try something like this in youre routes
resources :posts do
resources :comments do
member do
post 'vote'
end
end
member do
post :mark_archived
end
No need to build your own voting system. Take a look at the voteable_mongo gem
I'm creating a message-board site using ruby on rails.
I generated two scaffolds: Topic and Forum. Topic belongs_to Forum.
when the user is creating a new topic, I should pass the forum_id (a GET var). something like:
http://example.com:3000/topics/new/1
and then, when the user submit the form he passes back the forum_id with the POST request (through hidden html field?).
what is the right way doing it?
thanks
routes:
resources :forums
get "admin/index"
resources :posts
resources :topics
resources :users
match '/signup', :to => 'users#new'
get '/login', :to => 'sessions#new', :as => :login
match '/auth/:provider/callback', :to => 'sessions#create'
match '/auth/failure', :to => 'sessions#failure'
match '/topics/new/:id', :to => 'topics#new'
A good way to do it is to nest topics resources inside forums resources like this:
resources :forums do
resources :topics
end
Then in your TopicsController
class TopicsController < ApplicationController
def new
#forum = Forum.find params[:forum_id]
#topic = Topic.new
end
def create
#forum = Forum.find params[:forum_id] # See the redundancy? Consider using before_filters
#topic = #forum.topics.build params[:topic]
if #topic.save
redirect_to #topic
else
render action: :new
end
end
end
And finally in your views/topics/_form.html.erb:
<%= form_for [#forum, #topic] do |f| %>
# Your fields
<% end %>
Currently
Project101::Application.routes.draw do
match '/:id' => 'companies#show'
resources :companies do
resources :customers
resources :users
resources :categories
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Everything belongs to a company. Trying to create routes like www.example.com/:id/customers where :id is always the company id.
At the moment www.example.com/:id works but all url's are generated as /companies/:id/cusotmers.
Saw Rails 3 Routing Resources with Variable Namespace.
Is this the right way of doing this?
EDIT
Kept :as => :company to help generate the URL's, Links, etc a little easier for me. Sure others could do cleaner or better method. Also had to manually create the edit, destroy, new with different urls so I could use them in links if user was admin.
Project101::Application.routes.draw do
match '/' => 'companies#index'
match '/companies' => 'companies#index'
match '/:company_id' => 'companies#show', :as => :show_company
match '/companies/:id/edit' => 'companies#edit', :as => :edit_company
match '/companies/:id/new' => 'companies#new', :as => :new_company
match '/companies/:id/destroy' => 'companies#destroy', :as => :delete_company
scope '/:company_id', :as => :company do
resources :customers
resources :users
resources :categories
resources :services
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Then just used basic nested_resources for links, controllers and forms.
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_company
def current_company
if params[:company_id] != nil
#current_company ||= Company.find(params[:company_id])
else
#current_company = nil
end
return #current_company
end
end
Basic links
<%= link_to "Customers", company_customers_path(current_company) %>
links for specific customer
<%= link_to #customer.name, edit_company_customer_path(current_company, #customer) %>
Controllers look like
class CustomersController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def new
#company = current_company
#customer = #company.customers.new
end
def create
#customer = Customer.new(params[:customer])
if #customer.save
flash[:notice] = "Successfully created customer."
redirect_to company_customer_path(current_company, #customer)
else
render :action => 'new'
end
end
end
And finally my forms look like
<%= form_for [#company, #customer] do |f| %>
<%= f.error_messages %>
....
<% end %>
Yes, if you always want the routes to begin with the company id you can wrap them in a scope like this:
scope ":company_id" do
resources :customers
resources :users
resources :categories
resources :addresses
end
For example:
class UsersController < ApplicationController
def doSomething
end
def doSomethingAgain
end
end
Can I restrict the user pass a get method only to doSomething, but doSomethingAgain is only accept post method, can I do so?
class UsersController < ApplicationController
verify :method => :post, :only => :doSomethingAgain
def doSomething
end
def doSomethingAgain
end
end
You may specify in routes.rb
map.resources :users, :collection=>{
:doSomething= > :get,
:doSomethingAgain => :post }
You may specify more then one method
map.resources :users, :collection=>{
:doSomething= > [:get, :post],
:doSomethingAgain => [:post, :put] }
Here example
resources :products do
resource :category
member do
post :short
end
collection do
get :long
end
end
I think you'll be best off with using verify as Draco suggests. But you could also just hack it like this:
def doSomethingAgain
unless request.post?
redirect_to :action => 'doSomething' and return
end
# ...more code
end