Rails , cant view register user page - ruby-on-rails

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.

Related

Using devise method 'current_user' in mailer

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!

Empty list of attributes to change on Ruby on Rails

I am trying to submit a grid form like the below one,
show.html.erb
<%= form_for :datadef, url: update_all_vmodule_path do |fdf|%>
<table id="csvfiles" class="display" width="100%">
<thead>
<tr>
<th width="10%">Column No</th>
<th width="20%">Column name</th>
<th width="20%">Data type</th>
<th width="15%">Column format</th>
<th width="10%">Length</th>
<th width="25%">Comments</th>
</tr>
</thead>
<tbody>
<% #datadefrecords.each do |ddre| %>
<%= fields_for "datadef_records[]", ddre do |dr_fld| %>
<tr>
<td><%= ddre.column_no %></td>
<td><%= ddre.column_name %></td>
<td><%= dr_fld.collection_select :column_datatype, Datadef.select(:column_datatype).uniq, :column_datatype, :column_datatype, {:prompt => true} %> </td>
<td><%= dr_fld.text_field :column_format %></td>
<td><%= dr_fld.text_field :column_length %></td>
<td><%= dr_fld.text_field :comments %></td>
</tr>
<% end %>
<% end %>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><%= fdf.submit 'Save' %> <%= link_to("<button>Cancel</button>".html_safe, cancelmodal_vmodule_path ) %></td>
</tr>
</tbody>
</table>
<% end %>
<% end %>
This the method i am calling it to submit
def update_all
session[:datadef] = nil
#updatedatadef = Datadef.all.where(:id => params[:datadef_records]).update_all(update_all_params)
redirect_to migproject_path(params[:vmodule][:migproject_id])
end
This is the strong parameters
def update_all_params
params.require(:datadef_records).permit( :column_datatype, :column_format, :column_length, :comments)
end
This is how i get the parameters from form
{"utf8"=>"✓",
"authenticity_token"=>"QrX8JYYYQOlfwKgAABJd0+A7VpugS2Y6n8doDsuKqeM=",
"datadef_records"=>{"12"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"},
"13"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"}},
"commit"=>"Save",
"id"=>"2"}
But i get this "Empty list of attributes to change" error which is not allowing me to write it in table. And i am not able to identify what could be the error.
Thanks in advance.
I think the problem is to do with your use of update_all, although I'm not 100% sure as to the syntax of the problem.
From what I understand, update_all needs a hash of actual params to populate your data; even then, it will only update what you pass to it (kind of like update_attributes).
Here's a good reference:
data = params[:datadef_records]
#updatedatadef = Datadef.update(data.keys, data.values)
This negates the use of your strong_params, which I'd recommend using. I'd have to spend some time thinking about getting it to work.

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

Pass in ID from previous view/controller

I have been fighting this for a while.
I have a simple app that use users, timesheets and entries. timesheets belong to to users and an user has many time sheets. Entries belong to time sheets.
A user can click on a time sheet and be presented with the entries from that time sheet. I can create entries in the rails console and they show up correctly. But I am having trouble inserting the correct timesheet ID on the entries.
I may not be doing this correctly, but I have a link on the time sheet view to the entry view. I am having trouble bringing over the time sheet id to insert it in the the create entry view.
I am using a form_for that looks like this.
<%= form_for(:entry, :url => {:action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.text_field(:time_sheet_id, :value => #sheet_id ) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
This is my entry controller.
def create
#time_id = TimeSheet.find(1)
#new_entry = Entry.new(entry_params)
if #new_entry.save
flash[:notice] = "New Entry has been Added!"
redirect_to(:action => 'index')
else
render('new')
end
end
Do instead:
#new_entry = time_id.build_entry(entry_params)
Sorry, I Am closing this.
It was poorly worded and I figured out the problem.
All I needed to do is run the form_for from my timesheets controller and redirect it to the entries controller.

Get id of an object to pass into form_for

I am trying to find my time sheet id to pass into a form_for
here is my controller
def show
#time_id = current_user.time_sheets.find(params[:id])
if current_user
#current = current_user.time_sheets
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
This is my form_for in my view:
<%= form_for(:entry, :url => {:controller => Entry, :action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.hidden_field :time_sheet_id, value: #time_id.id %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
What needs to happen is that the timesheet id needs to get passed into the form_for so the entry can have the timesheet id.
I have user, timesheets and entries. user has_many time sheets, timesheets belongs to users and users have many time sheets. Entry belongs to timesheet.
I am getting this error "Couldn't find TimeSheet without an ID"
You should use "Nested Resources", eg. see here: Rails 3: How to create a new nested resource?
You could use form_for([#timesheet, #entry]) to pass the id of the timesheet without using a hidden field. If you do this, the :url param also become obsolete.
Try adding:
<%= #time_id.id %>
to output your variable and make sure it's exactly what you think it is. If #time_id is a timesheet, you should probably rename that variable to #time_sheet.

Resources