I'm getting error "uninitialized constant SongsController" on ruby on rails - ruby-on-rails

I'm getting an error like that, I'm new in rails.
my routes.rb
Rails.application.routes.draw do
get 'welcome' => 'page#search'
resources :songs
end
search.html.erb
<%=form_for #song do |f|%>
<%=f.text_field :search%>
<%=f.submit 'Search'%>
<%end%>
page_controller.rb
class PageController < ApplicationController
attr_accessor :a
def search
#songs = Song.all
#song = Song.new
end
def new
#song = Song.new
end
def Create
#song = Song.new()
if #song.save
rediect_to ''
else
render new
end
end
def parameter
params.require(#song)
end
end
path
welcome_path GET /welcome(.:format) page#search
song_new_path POST /song/new(.:format) song#new
songs_path GET /songs(.:format) songs#index
POST /songs(.:format) songs#create
new_song_path GET /songs/new(.:format) songs#new
edit_song_path GET /songs/:id/edit(.:format) songs#edit
song_path GET /songs/:id(.:format) songs#show
PATCH /songs/:id(.:format) songs#update
PUT /songs/:id(.:format) songs#update
DELETE /songs/:id(.:format) songs#destroy

you declared resources :songs in your routes file, so Rails expecte SongsController inheriting from ApplicationController in your app/controllers folder. If you do not have this controller, create new file:
app/controller/songs_controller.rb
class SongsController < ApplicationController
# add implementation of CRUD methods
def index
end
def show
end
def new
end
# ...
end

Rename your controller as SongsController

Related

Why am I getting a recordnotfound error when trying to access an instance in rails?

I have a user profile controller called "userinfo" and it's corresponding view. The userinfo index is the root path. In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. It is giving me this error when I go to the home page:
My routes are:
My userinfos_controller:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#userinfors = Userinfo.find(params[:id])
end
def show
#myvideo = Video.last
end
def new
#userinformation = current_user.userinfos.build
end
def create
#userinformation = current_user.userinfos.build(userinfo_params)
if #userinformation.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
#userinformation.destroy
redirect_to userinfo_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major)
end
def find_userinfo
#userinformation = Userinfo.find(params[:id])
end
end
and my view is:
<%= link_to 'profile', userinfors_path(#userinfors) %>
My routes.rb file:
Rails.application.routes.draw do
devise_for :users
resources :userinfos do
resources :videos
end
resources :pages
get '/application/decide' => 'application#decide'
root 'userinfos#index'
get '/userinfos/:id', to: 'userinfos#show', as: 'userinfors'
end
Thanks for any help!
ok, there are multiple errors and you are not following conventions of rails, index is not for what you have used.
Index is used to list all the users and show for a particular one with id passed in params.
Your index path is, as you can see, /userinfos which is correct and it doesn't have any id with it but you are trying to find user with params[:id] which is nil and hence the error.
Lets try out this:
def index
#userinfors = Userinfo.all #pagination is recommended
end
In your index view,
<% #userinfors.each do |userinfor| %>
<%= link_to "#{userinfor.name}'s profile", userinfo_path(userinfor) %>
<% end %>
It should work now.
Please read routing and action controller to get the idea and understand the magic behind rails routing and mvc architecture..

Ruby on Rails: Controller Not Evaluating Method

The next comment button should be directed to http://localhost:3000/articles/14/comments/70.
What it currently executes is: http://localhost:3000/articles/14/comments/56/next_comment
How does one fix this?
#button
= link_to "next comment", next_comment_article_comment_path(#article, #comment)
#controller
class CommentsController < ApplicationController
def next_comment
#comment ||= #scope.next(#comment)
end
def scope
#scope ||= #article.comments
end
end
UPDATE
#routes.rb
resources :articles do
resources :comments do
member do
get 'next_comment'
end
end
end
Run rake routes and check if there is a route to next_comment_article_comment_path(#article, #comment)
And try whith: article_comment_next_comment_path(#article, #comment)

Rails namespace foo.all not working

I´m new to Rails and can´t find any solution to my problem with my namespace I created.
The problem is that if I like to show all my rooms under localhost:3000/manage/ it does not work.
error: undefined method `title' for nil:NilClass.
If I try to show all my rooms under ../manage/rooms/ it is still not showing all rooms I created error: can´t find id... if I use an id e.g. ../manage/rooms/38 it is working. Hope someone can help me.
So here we go:
I created a namespace like:
routes.rb:
resources :manage
namespace :manage do
resources :rooms
end
Rake routes:
manage_index GET /manage(.:format) manage#index
POST /manage(.:format) manage#create
new_manage GET /manage/new(.:format) manage#new
edit_manage GET /manage/:id/edit(.:format) manage#edit
manage GET /manage/:id(.:format) manage#show
PATCH /manage/:id(.:format) manage#update
PUT /manage/:id(.:format) manage#update
DELETE /manage/:id(.:format) manage#destroy
manage_rooms GET /manage/rooms(.:format) manage/rooms#index
POST /manage/rooms(.:format) manage/rooms#create
new_manage_room GET /manage/rooms/new(.:format) manage/rooms#new
edit_manage_room GET /manage/rooms/:id/edit(.:format) manage/rooms#edit
manage_room GET /manage/rooms/:id(.:format) manage/rooms#show
PATCH /manage/rooms/:id(.:format) manage/rooms#update
PUT /manage/rooms/:id(.:format) manage/rooms#update
DELETE /manage/rooms/:id(.:format) manage/rooms#destroy
a controller under ../manage/rooms_controller.rb
class Manage::RoomsController < ApplicationController
def index
#rooms = Room.all
end
def new
#room = Room.new
end
def create
#ender text: params[:rooms].inspect
#render text: Rails.logger.info(params[:rooms].inspect)
#room = Room.new(room_params)
if #room.save
redirect_to [:manage, #room]
else
render 'new'
end
end
def show
#room = Room.find(params[:id])
end
def edit
#room = Room.find(params[:id])
end
def update
#room = Room.find(params[:id])
if #room.update(params[:rooms].permit(:number_of_rooms, :occupancy, :room_type, :gender, :title))
redirect_to [:manage, #room]
else
render 'edit'
end
end
def destroy
#room = Room.find(params[:id])
#room.destroy
redirect_to manage_index_path
end
private
def room_params
params.require(:rooms).permit(:number_of_rooms, :occupancy, :room_type, :gender, :title)
end
end
and under ... manage/rooms/_index.html.erb it is rendered in /layouts/_navigation_left.html.erb
Thanks! If you need more information just ask.
Thanks sorry I´m quite new to Rails. How do I change the controller / routes so I can manage not just #rooms but even #foo, #fee ...? Thanks.
I try to use:
I do like to show <%= #room.title %> <%= #room.number_of_rooms %>or do you need something else? Sorry!
and <%= render 'manage/rooms/index'%>

Wicked gem not reaching step 1

I've been following step by step example for wicked gem https://github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step but I'm struggling to make it work
routes.rb
post '/trips/building/build(.:format)', :to => "trips/build#create"
resources :trips do
resources :build, controller: 'trips/build'
end
trips_controller.rb
class TripsController < ApplicationController
include Wicked::Wizard
before_action :set_trip, only: [:show, :update]
steps :basic, :details
def show
render_wizard
end
def create
#trip = Trip.create
redirect_to wizard_path(steps.first, :trip_id => #trip.id
end
def update
#trip.update_attributes(trip_params)
render_wizard #trip
end
private
def set_trip
#trip = Trip.find(params[:trip_id])
end
def trip_params
....
end
end
index.html.erb
<%= link_to 'Create New Trip', '/trips/building/build', :method => :post, :class=>'btn btn-danger'%>
error in console:
Started POST "/trips/building/build" for 127.0.0.1 at 2014-04-16 22:50:20 -0700
ActionController::RoutingError - uninitialized constant Trips
this is driving me crazy...
any thoughts?
Your controller is named incorrectly - your route is pointing to the Trips::BuildController but your controller is defined as the TripsController.
The link you shared defines a Products::BuildController so that's why it works there.

NoMethodError in controller#new - undefined method

I'm getting a NoMethodError in the new action of my business_controller.
It seems to be acessing the #business object for a form in my view and the error occurs then:
undefined method `businesses_path' for
Here is my new method:
def new
if Business.where(:user_id => current_user.id).first.blank?
#business = Business.new
else
redirect_to user_businesses_path(current_user.id)
end
end
My routes are:
resources :users do
resources :businesses
member do
get 'account'
post 'payment'
put 'update_password'
get 'membership'
end
end
mind.blank's suggested changes
before_filter :check_if_user_has_business, :only => :index
def new
#business = Business.new
end
private
def check_if_user_has_business
redirect_to new_user_business_path(current_user) unless current_user.business
end
Do you have a route businesses_path or only user_businesses_path? If you only have the second one then you should specify that URL in your form:
<%= form_for #business, url: user_businesses_path do |f| %>
Also, if you have the correct associations set up, then you can write your if statement as follows:
if current_user.business.nil? # since it's a has_one association
Here's how I would write it:
Class BusinessesController < ApplicationController
before_filter :check_if_user_has_business, only: :new
def new
#business = Business.new
end
private
def check_if_user_has_business
redirect_to user_businesses_path(current_user) if current_user.business
end
end

Resources