Rails 5, ruby 2.4.0
Error: rails while running rails test
Mysql2::Error: Column 'encrypted_password' cannot be null
My test: utilities_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
test "Has first name vidur" do
get users_update_address_url
assert_equal "vidur", users(:regular).first_name
end
end
My Fixture: users.yml
admin:
id: 1
first_name: admin
last_name: tukaweb
email: 'admin#email.com'
encrypted_password: <% Devise::Encryptor.digest(User, 'password') %>
regular:
id: 2
first_name: vidur
last_name: punj
email: 'vidur.punj#hotmail.com'
encrypted_password: <% Devise::Encryptor.digest(User, 'password') %>
You probably want to print the output of that Ruby code instead of just evaluating it, so you'll want to change your ERB delimiters from the evaluate-only to output.
In other words, change
<% %> to <%= %>
So in your case:
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
Related
This is not a homework. I am doing this to boost my RoR skills.
I have both Manufacturer and Model entity. I have all the relationship setup correctly and user has to choose Manufacturer before choosing Model. I added more functionalities to my application that I created. Now, I would like to filter out the name from the Model dropdown list where the name contains the word (Other).
Example for name attribute in Model entity: F20, F10 (Other), E90.
I would like to filter out the name of the model which contain (Other). SQL Statement would be Model.where.not("name LIKE ?", "%(Other)%"). After the manufacturer is selected, F20 and E90 will be shown. F10 (Other) will be excluded since the name contains the word (Other).
This is what I have for the drop down list form.
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, Manufacturer.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, Manufacturer.order(:name), :models, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//Some code here that are not related
<% end %>
What I have tried:
In my tints_controller, this and I have updated my form in order to work with what I have modified.
def quotation
#listmanufacturer = Manufacturer.order(:name)
#listmodel = Model.where.not("name LIKE ?", "%(Other)%")
render 'quotation'
end
My form
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, #listmanufacturer, :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, #listmanufacturer, #listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//Some code here that are not related
<% end %>
However, I received an error like TypeError: nil is not a symbol. I am unable to modify my models_controller since the drop down list is being used a few times in different view. Some views will need the models with the word (Other) and some will not require.
#listmodel is the part that breaks everything. By replacing :models with #listmodel, I am hopping to filter out the models name that contain (Other).
Any help is greatly appreciated!
Note: Up to this point, my application is working correctly. All relationships are setup correctly in model and routes are configured.
UPDATE, more logs
ActionView::Template::Error (#<ActiveRecord::Relation [#<Model id: 1, name: "Kancil Old", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 2, name: "Kancil 04", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 3, name: "Kembara", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 4, name: "Kenari", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 5, name: "Kelisa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 6, name: "Myvi 05", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 7, name: "Rusa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 8, name: "Viva", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 9, name: "Alza", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 10, name: "Myvi Ii", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, ...]> is not a symbol nor a string):
6: </div>
7:
8: <div class="field">
9: <%= f.grouped_collection_select :model_id, #listmanufacturer, #listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
10: </div>
11:
12: <div class="field">
app/views/tints/quotation.html.erb:9:in `block in _app_views_tints_quotation_html_erb__1724914643_132263140'
app/views/tints/quotation.html.erb:3:in `_app_views_tints_quotation_html_erb__1724914643_132263140'
app/controllers/tints_controller.rb:47:in `quotation'
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (9.0ms)
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (539.0ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from status_code_with_paginate at C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/will_paginate-3.1.0/lib/will_paginate/railtie.rb:49)
In my Manufacturer model,
class Manufacturer < ApplicationRecord
//Something here
def listmodel
models.where.not("name LIKE ?", "%(Other)%")
end
end
In my form,
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, Manufacturer.all.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, Manufacturer.all.order(:name), :listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//More code here
<% end %>
Now in my drop down list, model with name containing (Other) will be excluded.
im getting this error message when i submit signup form:
(im following railscast #250 http://railscasts.com/episodes/250-authentication-from-scratch)
Mysql2::Error: Column 'password' cannot be null:
INSERT INTO `users` (`activated`, `activation_key`, ... , `email`, `password`, `password_salt`, `telephone`, `updated_at`)
VALUES (0, '9cli91cjwmt1rcahaiyefh05xhwy5hvz', ..., 'name#domain.tld', NULL, '$2a$10$4vc0HksIm17rzKDxcYVQiO', 123456789, '2013-06-27 19:16:54')
**Parameters**
{"utf8"=>"✓",
"authenticity_token"=>"DNEv4SuXt5btf+MTqM1xA6BWeT7JUa3OAf4AITFCe18=",
"user"=>{"email"=>"name#domain.tld",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"telephone"=>"123456789",
"activation_key"=>"9cli91cjwmt1rcahaiyefh05xhwy5hvz"},
"commit"=>"Create User"}
Form
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
...**
i have password value FILTERED in parameter because i use f.password_field.. and therefore i have NULL in SQL command.. f.text_field in email works.. what am i missing ?
only different thing from railscast is i have this in UsersController but it should work
def create
params[:user][:activation_key] = Array.new(32){Random.new.rand(36).to_s(36)}.join
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
Thank you
EDIT:
in Debug #user.inspect
throw this value
#<User id: nil, address_id: nil, company_id: nil, email: "username#domain.tld", password: nil, telephone: 123456789, activation_key: "jolkxv5bd7c2zgq08tkvb7srwe20tfqc", created_at: nil, updated_at: nil, activated: false, password_salt: nil, password_hash: nil>
i had commented
before_save :encrypt_password
now password_hash and password_salt are generated but password column still NULL :(
i tried new app from scratch and it works.. but im not able to solve this ?
I don't know what exactly is going wrong with your application, but the Railscast you are referring to is kinda dated because nowadays you would use has_secure_password. There is a revised episode of that Railscast, but only paying customers can see the revised episodes.
Of course, nothing keeps you from doing authentication without that new method, and I don't think there is any particular disadvantage in doing so, but if you want to check out how has_secure_password works, look at this little tutorial.
ok i got it, this railscast doesnt use password column ! it uses only: email:string, password_hash:string, password_salt:string
I have a User model which has_many :portfolios and each Portfolio has_many :pictures. My routes look like this:
resources :users do
resources :portfolios do
resources :pictures
end
end
Then I try to create a new Portfolio like this:
<%= link_to "new portfolio...", new_user_portfolio_path(current_user) %>
With a form like this:
<%= form_for [current_user, #portfolio], html: { class: "form_settings" } do |f| %>
...
<% end %>
But when I click on "new portfolio..." my URL says /users/12/portfolios/new and I get this error:
No route matches {:action=>"show", :controller=>"portfolios", :user_id=>#<User id: 12, name: "michael", password_digest: "d787f56b080945c1ec0b3343cbf962ca427bb8ef", remember_token: "dL4nPlt0E5azUMemNIvkdg", admin: false, created_at: "2013-03-03 01:18:19", updated_at: "2013-03-03 19:56:28">, :id=>#<Portfolio id: nil, user_id: 12, name: nil, created_at: nil, updated_at: nil>}
I don't think the error is coming from the form to create the portfolio. There must be a link on the /portfolios/new page to add a picture or something like that, and this is what's failing (because the portfolio is a new record).
You need to hide this link when you're on the new page.
Today i have a little problem containing a File Upload.
First some Infos:
Rubyversion: 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
Railsversion: Rails 3.1.1
Needed Code:
My Form
<%= form_for #label, :html => { :multipart => true } do |f| %>
<% if #label.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#label.errors.count, "error") %> prohibited this label from being saved:</h2>
<ul>
<% #label.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :file %><br />
<%= f.file_field :file %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My Model:
class Label < ActiveRecord::Base
attr_accessor :file
attr_reader :file
def file=(file)
log = Logger.new(Rails.root.join("log/label.log").to_s)
log.info "file-Action called"
path=file.tempfile.to_path.to_s
filename="labellist.csv"
dest=Rails.root.join("app/assets/csv/").join(filename).to_s
FileUtils.cp(path, dest)
csv=CsvLabelParser.new
log.info "New Parser initialized!"
csv.parse_csv(dest)
log.info "Variables: path: #{path.inspect} <-----> dest: #{dest.inspect}"
end
end
My Parser
class CsvLabelParser
require 'csv'
def initialize
#logger=Logger.new(Rails.root.join("log/parser.log").to_s)
end
def logger
#logger
end
def parse_csv(path)
counter = 0
read_handle = File.open(path, "rb")
content = read_handle.read
self.logger.info "CONTENT: #{content.inspect}"
read_handle.close
content.each_line do |line|
if counter != 0
csv_array=line.split(";")
self.logger.info "CSV-Array No. #{counter}: #{csv_array.inspect}"
label=Label.new
label.labnr=csv_array[10]
label.name=csv_array[0]
label.firm1=csv_array[3]
label.firm2=csv_array[4]
label.postal=csv_array[7]
label.city=csv_array[8]
label.country=csv_array[9]
label.street=csv_array[5]
label.pob=csv_array[6]
label.save
end
counter += 1
end
end
end
Controller-Log
# Logfile created on 2012-06-06 09:49:23 +0200 by logger.rb/31641
Controller-create-method called! => variables: Params: {"utf8"=>"✓", "authenticity_token"=>"L+fpIMqFA9qe9U/LxU+atFONT8e3L5xEUum1321mRng=", "label"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x00000003df2318 #original_filename="ADRBW076-Q.csv", #content_type="text/csv", #headers="Content-Disposition: form-data; name=\"label[file]\"; filename=\"ADRBW076-Q.csv\"\r\nContent-Type: text/csv\r\n", #tempfile=#<File:/tmp/RackMultipart20120606-2741-1izzofa>>}, "commit"=>"Create Label", "action"=>"create", "controller"=>"labels"}<-----> Label: #<Label id: nil, labnr: nil, name: nil, firm1: nil, firm2: nil, postal: nil, city: nil, country: nil, street: nil, pob: nil, created_at: nil, updated_at: nil>
So! As you can see i want to upload a CSV-file to parse it into my database. A copy of the File should be stored in a folder called CSV for later use. I've done that about 5 times before and it worked well, but this time it seems, that the file-method in the model isn't called.
For now i only want it to work nothing more there will be some changes to catch errors and stuff like that in the future ;).
I've logged the Controller action, which is called every time. May the Logfile can Help you! For me it looks okay, comparing it to other logfiles.
I've searched for a solution, but everything looks okay to me (maybe i've just forgotten a : or a ,), so i can't really say what's wrong!
Thanks for your help
I have the Solution!
I am using Rails 3.1.1 with Ruby 1.9.3 and it's gemset. The Problem was the new feature "attr_accessible". Normally not used in Rails 3.1.1 it became necessary to make the :file-Field accessible due to the newer gemset. So the Solution is simple, I just had to add:
attr_accessible :file, ect
To the Top of my model and everything worked fine!
I have a Client and ProposalRequest model that look like this:
class Client < ActiveRecord::Base
has_many :proposal_requests
accepts_nested_attributes_for :proposal_requests, :allow_destroy => true
end
class ProposalRequest < ActiveRecord::Base
belongs_to :client
end
In my my routes file, I included the nested routes, as usual.
resources :clients do
resources :proposal_requests
end
And this is my form so far:
-semantic_form_for [Client.new, ProposalRequest.new] do |f|
=f.inputs
=f.buttons
But after this, I'm stuck because of this error.
No route matches {:controller=>"proposal_requests", :client_id=>#<Client id: nil, name: nil, title: nil, organization: nil, street_address: nil, city: nil, state: nil, zip: nil, phone: nil, email: nil, status: "interested", how_you_heard: nil, created_at: nil, updated_at: nil>}
Can anyone help me puzzle out this error?
The problem is that your nested route is meant to add a new ProposalRequest to an existing Client. If you want to create a Client and a ProposalRequest at the same time, you need to just use new_client_path and semantic_form_for #client do |f|.
I would recommend you do the following in your clients_controller:
def new
#client = Client.find(params[:id])
#client.proposal_requests.build
end
And in your view:
semantic_form_for #client do |f|
= f.inputs # fields for client
= f.inputs :name => 'Proposal Request', :for => :proposal_requests do |pf|
= pf.input :some_proposal_request_attribute
= f.buttons
Hope this helps. Make sure to look at all the examples at https://github.com/justinfrench/formtastic and do some trial and error to get your form how you want it.