Regarding rails testing with Capybara &Rspec ...capybara element not found - ruby-on-rails-4

I have two models in my rails app let say company model and lead model to add leads. I have to test lead form while adding new lead. so I used integration testing . I have fields on my form through which i updated the db too but capybara errors element not found.my code as folows
new lead form
<title>Add Contact</title>
<h1> Enter Lead information</h1>
<%= form_for #lead, :url => lead_path(#lead.id), :html => { :multipart =>true } do|f|%>
<p>
<%= f.label :Name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :Linkedln_Address, 'Linkedln_Address' %><br>
<%= f.text_field :linkedln_address %>
</p>
<p>
<%= f.label :Twitter_Feed, 'Twitter_Feed'%><br>
<%= f.text_field :twitter_feed %>
</p>
<p>
<%= f.label :avatar, "Image File" %>
<%= f.file_field :avatar %>
</p>
<p>
<%= f.label :Notes, 'Notes' %><br>
<%= f.text_area :notes %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>'`
my spec is
spec/requests/leads_spec.rb
`require 'spec_helper'
describe "POST /lead", type: :feature do
it 'creates and saves valid lead' do
visit "/lead/new"
fill_in 'Name', :with => "abcdefg"
fill_in 'linkedln_address', with: 'lmnop#gmail.com'
fill_in 'twitter_feed', with: 'www.twiitter.com/microsoft'
fill_in 'notes', with: 'ajkhd akjf \n alskdfh alsjdfh'
click_button 'create Lead'
current_path.should ==lead_path
page.should have_content 'The Lead is Successfully Added'
within 'h1' do
page.should have_content "abcdefg"
end
page.should have_content "lmnop#gmail.com"
page.should have_content "www.twiitter.com/microsoft"
page.should have_content "ajkhd akjf \n alskdfh alsjdfh"
end
end
`

Capybara is case sensitive, your button is probably 'Create Lead' and not 'create Lead' (should also verify other fields)

Related

errors messages doesn't work in HTML ruby code

I am using rails 4.1.6 I looked at the active record validations site and follow their direction, but nothing is displayed in the HTML even if there is an error.
However, when I do it in rails console it works.
post = Post.new #create an empty post to test
post.valid? #false
post.errors.messages #this is successfully generate the error message array
However, it doesn't display any error messages in HTML. In fact "#post.errors" doesn't even run
-Ruby code in html
<%= form_for #post, :method => :post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit "Submit" %>
<% if #errors.any? %>
<ul>
<% #errors.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<% end %>
-My PostsController
def create
# post = Post.new(title: params[:post][:title], url: params[:post][:url])
post = Post.new(post_params)
if post.save
redirect_to posts_path
else
#errors = post.errors.messages
redirect_to paths_path
end
end
private
def post_params
params.require(:post).permit(:title, :url)
end
-My post model
class Post < ActiveRecord::Base
validates :title, :length => {maximum: 140, minimum:1}, :presence => true
validates :title, :length => {maximum: 2083, minimum:1}, :allow_blank => true
end
When it comes to errors I do like to have a partial so I can keep my errors the same in the whole app like so:
app/views/shared/_errors.html.erb:
<% if object.errors.any? %>
<h5>The <%= t("activerecord.models.#{object.class.to_s.downcase}") %> could not be saved due to the following errors:</h5>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
And then for the views you want the errors to be displayed, just call:
<%= render 'shared/errors', object: #your_object %>
Hope this helps!
thanks for your help. Got some tip from my teacher it work. Simply use flash[:message] and it worked. Sorry for the trouble everyone
-My controller code
def create
post = Post.new(post_params)
if post.save
redirect_to :back
else
flash[:message] = post.errors.messages
redirect_to :back
end
end
-My HTML code
<%= simple_form_for #post, :method => :post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit "Submit" %>
<%= flash[:message] %>
<% end %>

ruby on rails: empty database records

I am new to Ruby on Rails. I am building a basic application for learning purpose.
I build a form and when I use the Save button, a record is created but it is a empty record with only an Id.
I hope someone can help me.
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :relaties
root 'welcome#index'
new.html.erb
<h1>Nieuwe Relatie</h1>
<%= form_for :relatie, url: "/relaties" do |f| %>
<p>
<%= f.label :naam %><br>
<%= f.text_field :naam %>
</p>
<p>
<%= f.label :straatnaam %><br>
<%= f.text_field :straatnaam %>
</p>
<p>
<%= f.label :huisnummer %><br>
<%= f.text_field :huisnunmmer %>
</p>
<p>
<%= f.label :postcode %><br>
<%= f.text_field :postcode %>
</p>
<p>
<%= f.label :plaats %><br>
<%= f.text_field :plaats %>
</p>
<p>
<%= f.label :omschrijving %><br>
<%= f.text_area :omschrijving %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
relaties_controller.rb
class RelatiesController < ApplicationController
def new
#relatie = Relatie.new
end
def create
#relatie = Relatie.new(params[:relatie])
#relatie.save
redirect_to(#relatie)
end
private
def relatie_params
params.require(:relatie).permit(:naam, :straatnaam, :huisnummer, :postcode, :plaats, :omschrijving)
end
def show
#relatie = Relatie.find(params[:id])
end
end
While creating/ updating a record you should whitelist the attributes that you would like to be saved in database. You have added a method named relatie_params to do that BUT you have not used it in the code which is why when you try to create a new Relatie record none of the attributes are stored in database as they are not white-listed.
In create action, replace
#relatie = Relatie.new(params[:relatie])
with
#relatie = Relatie.new(relatie_params)
For your reference, read about Strong Parameters which was introduced in Rails 4

Ruby on Rails Guides | partial variable - cannot contain nil or be empty

I am following Ruby on Rails Guides and I have an error in rendering partial when I want to create a new article:
First argument in form cannot contain nil or be empty
Marked error is from partial _form.html.erb located in the same place as index:
<%= form_for #article do |f| %>
index.html.erb has link:
<%= link_to 'New article', new_article_path %>
controller:
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
form partial:
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %><br>
</p>
<% end %>
new.html.erb which renders partial:
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
your controller needs a new method
def new
#article = Article.new
end
that way there's an #article object that the form_for can reference.
When you follow a new link, the new action is executed and the form is displayed. It's only when the form is submitted that the create action is executed.

Rendering sign-in view giving error

Controller code
class AuthenticationController < ApplicationController
def sign_in
#user=User.new
end
def login
username_or_email=params[:user][:username]
password=params[:user][:password]
#check for the username or email
if username_or_email.rindex('#')
email=username_or_email
else
email=username_or_email
end
if User.authenticate(email,password)
flash[:notice] = 'Welcome.'
redirect_to :root
else
flash.now[:error] = 'Unknown user. Please check your username and password.'
render "sign_in"
end
Sign-in view Code
<p>Sign In</p>
<%= form_for #user, :as => :user, :url => sign_in_path(#user) do |f| %>
<p>
<%= f.label 'username or email:' %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label 'password:' %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.submit 'Sign In' %>
<%= f.submit 'Clear Form', :type => 'reset' %>
</p>
<% end %>
Error:-
First argument in form cannot contain nil or be empty when I am rendering Sign_In form after the user is not authorised.

First argument cannot contain nil or be empty rails 4

I'm making a single page application based on Futureme.org for practice. The user goes to the home page, sees a form to put their email address, subject, and the body of their message and sends it.
The problem I am having is I get an error "First argument in form cannot contain nil or be empty". Here is my code;
Model;
class Letter < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-,]+#[a-z\d\-.]+\.[a-z]+\z/i
validates_presence_of :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates_length_of :subject, presence: true, length: { maximum: 50 }
validates_presence_of :message
end
Controller;
class LettersController < ApplicationController
def new
#letter = Letter.new
end
def create
#letter = Letter.new(params[:letter])
if #letter.save
redirect_to letters_path, :notice => "Your letter was sent!"
else
render "new"
end
end
end
View form;
<%= form_for #letter, :html => {:class => 'form-horizontal'} do |f| %>
<% if #letter.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(#letter.errors.count, "error")%>stopped this message from being saved</h2>
<ul>
<% #letter.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %><br />
</div>
<div class="field">
<%= f.label :message, "Message" %><br />
<%= f.text_area :message, size: "60x10" %>
</div>
<div class="actions"><%= f.submit "Submit", class: "btn btn-small btn-primary" %></div>
<% end %>
The form is on the home page which is in the "Welcome Controller".
Any help would be great.
It looks like you build letter in action new when form is drawn on other view :)
You should move #letter = Letter.new to appropriate action
One of the variant is:
#WelcomeController
def home
#letter = Letter.new
end
#LettersController
def create
#letter = Letter.new(params[:letter])
if #letter.save
redirect_to letters_path, :notice => "Your letter was sent!"
else
render "welcome/home"
end
end
be careful if you prepare some data in action home you should care about initializing them for action create when validation failed because you render "welcome/home" view