I've been banging my head against this for a while. I know it's a simple problem, and I've reviewed other code examples where I'm doing this successfully, but, I'm completely stumped.
I'm getting an error "undefined method `page_leads' for nil:NilClass" when I try to go to the "Show" page. On my landing_pages "show" page I'm trying to show the leads that came in via that page.
My show page code for this is:
<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Score</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #landingpage.page_leads.each do |page_lead| %>
<tr>
<td><%= page_lead.fname %></td>
<td><%= page_lead.lname %></td>
<td><%= page_lead.score %></td>
<td><%= link_to 'Show', page_lead %></td>
<td><%= link_to 'Edit', edit_page_lead_path(page_lead) %></td>
<td><%= link_to 'Destroy', page_lead, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
My landing_page model has:
has_many :page_leads
My page_lead model has:
belongs_to :landing_page
My controller code for the "show" method for both landing_pages and page_lead is:
def show
#landing_page = LandingPage.all(landing_page_params)
end
On my page_leads table I have the "landing_page_id" field so I can associate the landing page to the lead.
Any ideas what I'm doing wrong?
Thanks in advance
Your controller action does not load any instance of a model. You expect it to load an instance of a LandingPage (usually the one in params[:id] for a show action). So your controller should assign it:
# `GET /landing_pages/:id`
def show
#landingpage = LandingPage.find( params[:id] )
end
It is because your #landingpage instance variable is not defined when you run that code.
Basically, in your controller's action, you should have something like:
def show
#landinpage = ... # insert your definition here
# rest of your controller's action
end
Related
As working on the Active Record as i have work on different function for active reocrd like Avg, sum and count as it display working fine and also Chart,
but one things is baffle me and i still cannot get it working and it should be working fine, as i cannot get display all data list table like
<table id="dttb" class="table table-hover">
<thead>
<tr>
<th> full name </th>
</tr>
</thead>
<tbody>
<tbody>
<% #user.each do |user| %>
<tr>
<td><%= user.fullname %></td>
</tr>
<% end %>
</tbody>
</tbody>
</table>
as it should be working as the error is kept displayed
undefined method `each' for nil:NilClass
as I look up information and most of them are mention .each do, seems I am doing wrong as I have used
<%= User.count(:user) %>
and
<%= column_chart User.group(:provider).count(:user) %>
and it seems working fine as query function.
so I tried again with find_each
<% User.find_each do |user| %>
<tr>
<td><%= puts user.fullname %></td>
</tr>
<% end %>
and the error is gone but it does not display at the data and it's show blanks unless I put 'link_to' but they keep display like
and I have put on AdminController.rb
class AdminController < ApplicationController
before_action :authenticate_user!
def index
#user = User.all
#tools = Tool.all
end
end
seems I miss something, I have look google or stackover flow, most of them answer are very same as this code as I wrote
Update: as I am able to get some data like a phone number or email
Here is code i wrote
<% User.find_each do |user| %>
<tr>
<td><%= link_to user.id, user %></td>
<td><%= link_to user.email, user %></td>
<td><%= link_to user.created_at.strftime('%v'), user %></td>
<td><%= link_to user.fullname, user %></td>
<td><%= link_to user.phone_number, user %></td>
</tr>
<% end %>
but frustration with fullname as it should be displayed but it not
Set #user (or better #users) in the controller:
def index # or the actual action name
#users = User.all # or User.order(:fullname)
end
I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end
I'm currently using Rails 3.2 with the activerecord-oracle_enhanced-adapter gem. I am connecting to an already existing table in an already existing database. I have created the controller, model, and view files myself (they were not autogenerated).
When I start up a rails server and navigate to localhost:3000/devices, I get the following error - "undefined method `accept' for nil:NilClass".
The full stack trace is here:
http://pastebin.com/YfiM7W7S
My MVC for device are printed below:
Model:
class Device < ActiveRecord::Base
self.table_name="DEVICE"
self.primary_key="DEVICE_NAME_ID"
end
View:
<h1>Listing devices</h1>
<table>
<thead>
<tr>
<th>Device Name ID</th>
<th>Device Desc</th>
<th>Agg Flg</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #devices.each do |device| %>
<tr>
<td><%= device.device_name_id %></td>
<td><%= device.device_desc %></td>
<td><%= device.agg_flg %></td>
<td><%= link_to 'Show', device %></td>
<td><%= link_to 'Edit', edit_device_path(device) %></td>
<td><%= link_to 'Destroy', device, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Device', new_device_path %><head>
Controller:
class DevicesController < ApplicationController
before_filter :set_device, only: [:show, :edit, :update, :destroy]
# GET /devices
# GET /devices.json
def index
#devices = Device.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_device
#device = Device.find(params[:id])
end
end
Any help would be greatly appreciated. If you need any more information, please let me know.
I'm following the tutorial found here. It's simple, and I've followed the instructions exactly to step 6.7. At this point, I get the error
undefined method `each' for nil:NilClass
when I try to access index.html.erb on the rails server.
I know the database is working fine, because I can do everything mentioned in step 6.3, create new posts and show/edit/destroy them with absolutely no problems.
Specifically, the issue is with the line
<% #posts.each do |post| %>
and it's essentially claiming that #posts is nil.
I appreciate any help for this ROR newbie! Thanks.
index.html.erb
<h1>Hello, Rails!</h1>
<table>
<tr>
<th>Name</th>
<th>Title</th>
<th>Content</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "My Blog", posts_path %>
posts_controller.rb
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
In order for the views to work fine in Rails they must be inside the correct directory. This is one of the many implementations of the so called "Convention over Configuration" that Rails loves.
So, if you have a method index and this method is inside a controller named PostsController, you must have a view named index inside the directory views/posts/. This way, Rails will know that it have to render this view when a get to this method is processed.
About a good tutorial, I would recommend this one. It is extense and covers a lot of things that are not just related to Rails itself, like deploying on Heroku and a little CSS.
I keep getting this error when I try and view my register user page
Firefox has detected that the server
is redirecting the request for this
address in a way that will never
complete.
def register
#User registration form
#user = User.new(params[:user])
if #user.save
flash[:notice] = "Account Created Successfully"
redirect_to(:action=>'menu')
else
flash[:notice] = "Please fill in all fields"
redirect_to(:action=>'register')
end
end
<div class="user new">
<h2>Create User</h2>
<%= form_for(:user, :url => {:action => 'register'}) do |f| %>
<table summary="User form fields">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name) %></td>
</tr>
<tr>
<th>UserName</th>
<td><%= f.text_field(:user_name) %></td>
</tr>
<tr>
<th>Password</th>
<td><%= f.text_field(:password) %></td>
</tr>
<tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email) %></td>
</tr>
<tr>
<tr>
<th>Telephone</th>
<td><%= f.text_field(:telephone) %></td>
</tr>
<tr>
<td> </td>
<td><%= submit_tag("Register") %></td>
</tr>
</table>
<% end %>
</div>
Aren't you putting a redirect in your view method?
redirect_to(:action=>'register')
Are you distinguishing between the view method and the method that is actually fired when the user submits the form? Ex. for RestfulAuthentication the view is called "new", and the actual method that creates a user after the form is submitted is called "create".
You're redirecting you the same register action in the else. Try render :action => '<action that contains the form>'
I think that FireFox message means that you have some kind of server redirect loop going on. For example, if you have a before filter in your application controller that says if the user in not logged in then send them to the register user page. But then on redirect to the register page, that user is still not logged in. Then Rails will try to redirect them again to that page. Thus forming a loop.