So I've done some research on Stack Overflow and tried a variety of solutions but nothing seems to be working. I'm trying to render my ListsController show page, but I keep getting the error : undefined method `name' for nil:NilClass. When I use the rails console, my params are passing the ID through, and I can call #list = List.find(params[:id] and then #list.name. However, I keep getting the same error message. My controller looks like this:
class ListsController < ApplicationController
def index
#lists = List.order("created_at DESC")
#list = List.new
end
def create
#list = List.new(list_params)
if #list.save
if request.xhr?
# somehow only send back the html for the page update
render :layout => false
else
redirect_to root_path
end
else
#lists = List.all
render :index
end
def show
#list = List.find(params[:id])
end
end
private
def list_params
params.require(:list).permit(:name)
end
end
My view currently looks like this:
<section class="todoapp">
<header class="header">
<h1><%= #list.name %></h1>
</header>
<section class="main">
<input class="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<%= render #list.items %>
<!-- exactly the same as: -->
<%#= render :collection => #list.items, :partial => "items/item" %>
</ul>
</section>
</section>
I've tried using the line <%= #list.name if #list.name %> but that doesn't seem to be ruling out the event of nil class either (even though params currently have an ID).
class ListsController < ApplicationController
def index
#lists = List.order("created_at DESC")
#list = List.new
end
def create
#list = List.new(list_params)
if #list.save
if request.xhr?
# somehow only send back the html for the page update
render :layout => false
else
redirect_to root_path
end
else
#lists = List.all
render :index
end
end
def show
#list = List.find(params[:id])
end
private
def list_params
params.require(:list).permit(:name)
end
end
Related
My pages controller has this code:
class PagesController < ApplicationController
def index
#pages = Page.all
end
def new
#page = Page.new
end
def edit
#page = Page.find(params[:id])
end
def create
#page = Page.new(page_params)
#page.save
redirect_to #page
end
def update
#page = Page.find(params[:id])
#page.update(page_params)
redirect_to #page
end
def show
#page = Page.find(params[:id])
end
def destroy
#page = Page.find(params[:id])
#page.destroy
redirect_to pages_path
end
private
def page_params
params.require(:page).permit(:title,:description)
end
end
And in my navbar I have this code:
<% #pages.each do |page| %>
<li?<%= link_to 'page.title', page_path(page)%></li>
<% end %>
The code should generate the titles of each page created, however I get the following error when I run the page:
undefined method `each' for nil:NilClass
I thought I had eliminated this issue by entering the code inside the new function in PagesController. This pages controller is just for additional pages. The navbar also contains links to static pages, so there should always be some value in the navbar.
Has anybody any advice?
Try this:
Add this in your application_controller.rb
#pages = Page.all
and in your navbar
<% #pages.each do |page| %>
<li><%= link_to "#{page.title}", page_path(page)%></li>
<% end %>
It sounds like you don't have any pages in your Page table. Ensure you have populated this table with some pages.
You can also wrap the code in an unless clause to check if #pages is nil.
For example:
<% unless #pages.nil? %>
<% #pages.each do |page| %>
<li?<%= link_to 'page.title', page_path(page)%></li>
<% end %>
<% end %>
Trying to render a to-do list with Ajax through Ruby on Rails.
Here is the index.html.erb snippet:
<div class="row">
<div class="col-md-7 col-md-offset-1" id="tasks"><%= render #tasks %></div>
</div>
The error raises when render #tasks hits.
The message states "Missing partial tasks/_task "
My controller declares that #tasks = Task.all as follows
class TasksController < ApplicationController
before_action :all_tasks, only: [:index, :create]
respond_to :html, :js
def index
#tasks = Task.all
end
def new
#task = Task.new
end
def create
#task = Task.create(task_params)
end
private
def all_tasks
#tasks = Task.all
end
def task_params
params.require(:task).permit(:description, :deadline)
end
end
Not sure what the issue is in this situation.
Any help appreciated.
It's asking for a task partial within your app/views/tasks folder.
To use or print #tasks you just call use it in your view using Ruby inside your views like <%= ruby_code %>
By default, controllers in Rails automatically render views with names that correspond to actions, it means if you use <%= render #tasks %>, Rails will try to find for some partial called tasks within the parent folder that's printing the current view.
Also I've seen you're assigning twice the value of #books in your index method, if you're using the before_action :all_tasks in your index then you don't need to "redeclare" it again.
Try with:
# app/views/index.html.erb
<div class="row">
<div class="col-md-7 col-md-offset-1" id="tasks">
<%= #tasks %>
</div>
</div>
# app/controllers/tasks_controller.rb
class TasksController < ApplicationController
before_action :all_tasks, only: [:index, :create]
respond_to :html, :js
def index
end
def new
#task = Task.new
end
def create
#task = Task.create(task_params)
end
private
def all_tasks
#tasks = Task.all
end
def task_params
params.require(:task).permit(:description, :deadline)
end
end
I am a newbie to rails, and I have already tried all solutions available on stackoverflow before.I am trying to access a user by id using localhost:3000/users/1. My code for user_controller is:
class UsersController < ApplicationController
def new
#user=User.new
end
def index
#users = User.all
end
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
#title = "Sign up"
render 'new'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation,:email)
end
def show
#user = #users.find(params[:id])
#users=User.all
end
end
And my show.html.erb contains :
<% provide(:title, #users.name ) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #users.name %>
</h1>
</section>
</aside>
</div>
Take show action out of private. It is not a private method. That is why the variable, #users is nil. However, you don't need to even find #users, answer below shows that.
You are trying to call the method name (an attribute), on an array, #users. You need to call it on the object, #user.
Side note: there is also a predefined method, name, in Rails for an ActiveRecord::Relation (User.all). however, in the example you posted, your variable, #users was nil, so it wasn't getting this far. But, if your show action was not a private method, you would have experienced this:
$ User.all.name # or #users.name
# => 'User'
$ Post.all.name
# => 'Post'
$ User.first.name # or #user.name
# => 'John'
Solution
# show.html.erb
<%= #user.name %>
# users_controller
class UsersController < ApplicationController
def index
#users = User.all
end
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
#title = "Sign up"
render 'new'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation, :email)
end
end
I'm building my photography portfolio in Rails 4.1.6 and Ruby 2.0.0p576.
I have 'collections' which have many 'photos'.
I am using dragonfly 1.0.10 for image uploads.
Collections Model
class Collection < ActiveRecord::Base
belongs_to :admin
has_many :photos
end
Photos Model
class Photo < ActiveRecord::Base
belongs_to :collection
extend Dragonfly::Model
dragonfly_accessor :image
end
Collection Controller
class CollectionsController < ApplicationController
respond_to :html, :xml, :json
def index
#collections = Collection.all
end
def new
#collection = Collection.new
respond_with (#collection)
end
def create
#collection = Collection.new(collection_params)
#collection.save
respond_with (#collection)
end
def edit
#collection = Collection.find(params[:id])
end
def update
#collection = Collection.find(params[:id])
if #collection.update(collection_params)
redirect_to #collection
else
render 'edit'
end
end
def show
#collection = Collection.find(params[:id])
end
def destroy
#collection = Collection.find(params[:id])
#collection.destroy
respond_with (#collection)
end
private
def collection_params
params.require(:collection).permit(:name, :desc, photos: [:image] )
end
end
Photos Controller
class PhotosController < ApplicationController
def new
#collection = Collection.find(params[:id])
#photo = #collection.new
end
def create
#collection = Collection.find(params[:collection_id])
#photo = #collection.photos.create(photo_params)
redirect_to collection_path(#collection)
end
private
def photo_params
params.require(:photo).permit(:image)
end
end
This is storing my photos for me correctly.
But on my collection show page, I'm getting a "NoMethodError in Collections#show undefined method `image'.
In terminal, I get the following error:
ActionView::Template::Error (undefined method `image' for #):
Code for the show page is below:
collections/show.html.erb
<h1><%= #collection.name %></h1>
<p><%= #collection.desc %></p>
<% #collection.photos.each do |photo| %>
<div>
<%= image_tag #collection.photos.image.thumb('400x300#').url %>
</div>
<% end %>
I'm an absolute rails n00b and need some help as to how to fix this and be able to view all the photos of the collection on its show page.
Please help!
You have a wrong usage of the each loop:
<% #collection.photos.each do |photo| %>
<div>
<%= image_tag photo.image.thumb('400x300#').url %>
</div>
<% end %>
Read How does iteration work in Ruby?
I have implemented a save buttons for jobs and it work fine now i want to list some of the saved jobs in the jobs index page for this i have this code
<h3>Saved Jobs</h3>
<ul>
<% #user.saved_jobs.limit(5).order(:created_at).reverse_order.each do |saved_job| %>
<li><%= link_to saved_job.job.title, saved_job.job.url %>
<span class="delete_button">
<%= link_to "X", saved_job, :method => :delete, :remote => true %></span></li>
<% end %>
</ul>
<%= link_to "see all", saved_jobs_path %>
but when i want access to the jobs index page i get this error undefined method saved_jobs' for nil:NilClass
this my saved_jobs controller
class SavedJobsController < ApplicationController
before_filter :authenticate_user!
def index
#saved_jobs = SavedJob.find_all_by_user_id(current_user.id)
end
def create
#job = Job.find(params[:saved_job][:job_id])
current_user.save_job!(#job)
respond_to do |format|
format.html { redirect_to #job }
format.js
end
end
def destroy
#job = SavedJob.find(params[:id]).job
current_user.saved_jobs.find(params[:id]).destroy
respond_to do |format|
format.html { redirect_to #job }
format.js
end
end
end
and this is my user controller
class ProfilesController < ApplicationController
before_filter :authenticate_user!
def show
#user = User.find_by_slug(params[:id])
if #user
#posts = Post.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
def index
#users = user_scope.paginate(page: params[:page], per_page: 2)
end
private
def user_scope
current_user ? User.where.not(id: current_user.id) : User.all
end
end
The error suggests that you didn't set #user in your controller action. Can you post the code from your controller?
Update based on controller code:
Your index action in SavedJobsController does not set #user. When you then call #user.saved_jobs.limit(5)..etc... in the view #user is nil, rather than current_user or whatever.
Additionally, you have set #saved_jobs - why not just use that instead of #user.saved_jobs?
for instance:
#in SavedJobsController
def index
#saved_jobs = current_user.saved_jobs.limit(5).order('created_at DESC')
end
and then, in your view:
<% #saved_jobs.each do |saved_job| %>