Using devise method 'current_user' in mailer - ruby-on-rails

am relatively new to rails,i have been working with devise authentication gem for a while now,but my problem is,my app sends an email,with the firstname and lastname of the current user after a form is filled,but each time i try to send the email with the above stated parameters i get an error that says "undefined method current_user"
my mailer looks like this,note:its a separate controller that controls it.
<table>
<tr>
<td>name</td><td><%= current_user.firstname %><%=current_user.firstname %></td>
<tr>
<td>phone no.</td><td><%= current_user.phone %></td>
</tr>
<tr>
<td>location</td><td><%= #device.location %></td>
</tr>
<tr>
<td>device type</td><td><%= #device.device_type %></td>
</tr>
<tr>
<td>brand</td><td><%= #device.brand %></td>
</tr>
</table>
my controller action is this
def create
#user=current_user
#device = Device.new(device_params)
if #device.save
DeviceMailer.send_device.deliver
flash[:notice] = 'Request sent successfully,you will be
contacted shortly'
else
flash[:error] ='Request not sent,check details and sendagain'
render 'new'
end
end

current_user is a method that is available only to views and controllers. If you want to use it in a mailer or model, it will have to be explicitly used as an argument for the mailer method.
Inside the controller create method:
DeviceMailer.send_device(current_user, #device).deliver
Mailer method:
def send_device(user, device)
#user = user
#device = device
# More code as needed
end
Mailer view:
<table>
<tr>
<td>name</td>
<td><%= #user.firstname %><%= #user.firstname %></td>
</tr>
<tr>
<td>phone no.</td><td><%= #user.phone %></td>
</tr>
<tr>
<td>location</td><td><%= #device.location %></td>
</tr>
<tr>
<td>device type</td><td><%= #device.device_type %></td>
</tr>
<tr>
<td>brand</td><td><%= #device.brand %></td>
</tr>
</table>

current_user is set when you login. In order to force the user to login before the user can execute a controller action you need to have the following line at the top of your controller:
before_action :authenticate_user!

Related

Active record is not display table

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

How to render slightly difference partial in "index" page and "show" page

There is a model Company that has many DailyDatum.
I want to show the daily data in companies/:id/daily_data and daily_data/index. But in the company's page I don't want to show company.name column.
views/daily_data/_daily_datum.html.erb
<tr>
<td><%= daily_datum.company.name %></td>
# This company.name needs to be shown when the partial is called from daily data index.
<td><%= daily_datum.column1 %></td>
<td><%= daily_datum.column2 %></td>
</tr>
views/daily_data/index.html.erb
<table>
<thead>
<tr>
<th>Company Name</th>
<th>Daily Datum1</th>
<th>Daily Datum2</th>
</tr>
</thead>
<%= render #daily_data %>
</table>
views/companies/daily_data.html.erb
<table>
<thead>
<tr>
<!--<th>Company Name</th>-->
<th>Daily Datum1</th>
<th>Daily Datum2</th>
</tr>
</thead>
<%= render #daily_data %>
</table>
How should I handle situation like this? Does I need to create another partial HTML?
This might be overkill since you're only trying to conditionally render one single field, but the "right" approach would be to create a helper.
I'd recommend creating a helper to conditionally render one of two partials for #daily_data depending on the path.
companies_helper.rb
def is_companies_index_path?
current_page?(companies_index_url)
end
def is_companies_show_path?
current_page?(companies_show_url)
end
def render_appropriate_partial
render 'daily_data_a' if is_companies_index_path?
render 'daily_data_b' if is_companies_show_path?
end
Then in your views you can simply call:
<% render_appropriate_partial %>
And it will render the appropriate partial based on which route/url your on.
You can use Routing Paramters to know what is the current action/controller, so with an if condition you can add the company name or not.
<% if action_name == 'index' && controller_name == 'daily_data' %>
<th>Company Name</th>
<% end %>

undefined method `page_leads' for nil:NilClass

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

Rails convention for case statement in view

I am new to ruby and rails so thought I'd ask a question on conventions.
I have a view which produces a list of items in a table and I was asked to make a change and in doing so have added a case statement to the view which I don't think is the correct way of doing things so thought that I would double check.
The change I made was just to add a class to the tr depending on the value of the last table column.
list.rhtml
<table width="100%">
<tr>
<th style="width: 80px;">ID #</th>
<th>Organisation</th>
<th>Product</th>
<th>Carrier</th>
<th>Carrier Ref</th>
<th>Post Code</th>
<th>Status</th>
</tr>
<%= render :partial => 'circuit/list_item', :collection => #circuits %>
</table>
list_item.rhtml
<%
# code I have added
#tr_class = ''
case list_item.status
when 'Handover'
#tr_class = ''
when 'Unprocessed'
#tr_class = 'high_priority'
when 'Ceased'
#tr_class = 'low_priority'
else
#tr_class = ''
end
# end of newly added code
%>
<!-- the class part is new aswell -->
<tr class="<%= #tr_class %>">
<td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
<td><%= list_item.organisation.name if list_item.has_organisation? %></td>
<td><%= list_item.product_name %></td>
<td><%= list_item.carrier.name %></td>
<td><%= list_item.carrier_reference %></td>
<td><%= list_item.b_end_postcode %></td>
<td><%= list_item.status %></td>
</tr>
Is there a Rails pattern or convention that can get the case statement out of this view?
If understand properly your question, I think you should put the case statement inside a helper function:
app/helpers/list_helper.rb
module ListHelper
def tr_class_for_status(status)
case status
when 'Unprocessed'
'high_priority'
when 'Ceased'
'low_priority'
else
''
end
end
end
_list_item.rhtml
<tr class="<%= tr_class_for_status(list_item.status) %>">

Rails , cant view register user page

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.

Resources