Rails spree : How to override login partial using deface? - ruby-on-rails-4

I'm trying to override login page in spree using deface, but I can't access the login partial using the following code
Deface::Override.new(
:virtual_path => 'spree/shared/_login',
:name => 'override login',
:replace=> "body",
:text=> "<body><h1>loin<h1></body>",
:disabled => false
)
How ever this seems not to work for some reason, in the server output I get :
Deface: 1 overrides found for 'spree/shared/_login'
Deface: 'override login' matched 0 times with 'body'
Rendered /home/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spree_auth_devise-3.1.0/lib/views/frontend/spree/shared/_login.html.erb (50.3ms)
Rendered /home/user/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spree_auth_devise-3.1.0/lib/views/frontend/spree/user_sessions/new.html.erb within spree/layouts/spree_application (77.4ms)
I tried multiple selectors for css in the page with no result, I noticed the login partial is located in 'spree_auth_devise-3.1.0' sub directory 'not spree_frontend-3.1.0' as the rest of them, does anyone now how to deface the login page in spree or how to reference the right path for its partial ?

If you would like to replace all body, better override partial and not use Deface. It is great tool but not when you want to do so big change.
Error means that it not found body in partial, and thats true because there is no body.
You can use only
<%= form_for Spree::User.new, :as => :spree_user, :url => spree.create_new_session_path do |f| %>
<div id="password-credentials">
<p>
<%= f.label :email, Spree.t(:email) %>
<%= f.email_field :email, :class => 'form-control', :tabindex => 1, autofocus: true %>
</p>
<p>
<%= f.label :password, Spree.t(:password) %>
<%= f.password_field :password, :class => 'form-control', :tabindex => 2 %>
</p>
</div>
<p>
<%= f.check_box :remember_me, :tabindex => 3 %>
<%= f.label :remember_me, Spree.t(:remember_me) %>
</p>
<p><%= f.submit Spree.t(:login), :class => 'btn btn-lg btn-success btn-block', :tabindex => 4 %></p>
<% end %>

Related

Using hstore and saving new values for user

My user model contains values for things like:
name, email, phone, approved, etc..
The scope of the project I am working on includes custom user settings and for this reason I am using hstore (I dont want to create a user_settings table).
On the user edit view I want to create a checkbox called 'colors' and whether or not the checkbox is checked determines if that setting is set for the user or not.
I have already setup hstore as follows:
class AddHstore < ActiveRecord::Migration
def up
enable_extension :hstore
end
def down
disable_extension :hstore
end
end
And updated the User model as follows:
class AddSettingsToUser < ActiveRecord::Migration
def up
add_column :users, :settings, :hstore
end
def down
remove_column :users, :settings
end
end
This is essentially my user edit view:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, :class => "form-horizontal user" }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :phone_number %><br />
<%= f.text_field :phone_number %>
</div>
<div class="actions">
<%= f.submit 'Update', :class => 'btn btn-primary' %>
</div>
<% end %>
At this point I am unsure as to how to actually implement this functionality. The user edit page allows the user to change their name, phone and other values but how would I include a new value for the hstore settings?
Inside the form_for from where you are creating users, add your hstore fileds like below :
<%= form_for resource, ...do |f| %>
#...
<%= f.fields_for :settings do |settings| %>
<%= settings.text_field :field1 %>
<%= settings.text_field :field2 %>
<% end %>
#....
<% end %>
Then update your controller strong parameter method like below to permit these new fields.:
def user_params
params.require(:user)
.permit(
:name,
:email,
settings: [ :field1, :field2 ]
)
end
Now, you are done. Open your rails console and try some sample data like
User.create(
settings: { field1: 'data1', field2: 'data2' }
)

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 %>

Argument error in ActiveAdmin after change in devise sessions#new form

I am using Rails 4.0.4 and Ruby 2.1.1.
I made few changes in my devise login form after generating migration for username and find the ActiveAdmin is throwing following error.
Showing /Users/MyCom/.rvm/gems/ruby-2.1.1/bundler/gems/active_admin-3136ccb910e8/app/views/active_admin/devise/sessions/new.html.erb where line #8 raised:
wrong number of arguments (6 for 4..5)
Extracted source (around line #8):
<%= active_admin_form_for(resource, :as => resource_name, :url => send(:"#{scope}_session_path"), :html => { :id => "session_new" }) do |f|
f.inputs do
resource.class.authentication_keys.each { |key|
f.input key, :label => t('active_admin.devise.'+key.to_s+'.title'), :input_html => {:autofocus => true}
}
f.input :password, :label => t('active_admin.devise.password.title')
f.input :remember_me, :label => t('active_admin.devise.login.remember_me'), :as => :boolean if devise_mapping.rememberable?
Following is my code in views/devise/sessions/new
<div class="panel-body">
<div class="col-md-6">
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-group">
<%= f.label :username %>
<%= f.input_field :username, class: "form-control", :autofocus => true %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<div class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
</div>
<div class="form-group">
<%= f.submit "Sign in", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
I was able to fix doing the following.
I had to change my config/initializers/simple_form_bootstrap.rb file.
I had added following which I had to take it out.
inputs = %w[
CollectionSelectInput
DateTimeInput
FileInput
GroupedCollectionSelectInput
NumericInput
PasswordInput
RangeInput
StringInput
TextInput
]
inputs.each do |input_type|
superclass = "SimpleForm::Inputs::#{input_type}".constantize
new_class = Class.new(superclass) do
def input_html_classes
super.push('form-control')
end
end
Object.const_set(input_type, new_class)
end
This issue is discussed in details here.
https://github.com/gregbell/active_admin/issues/2703

Rails 4 NoMethodError

I have a problem and I don't realu understand it.
I have this error :
undefined method `value' for "test":String
here my view :
<div id="search">
<%= form_for :search, url: search_path do |f| %>
<%= f.text_field :value, :class => 'form-control input-sm', :placeholder => 'Search...' %>
<%= button_tag '<i class="fa fa-search"></i>'.html_safe, :id => 'search-btn', :class => 'btn' %>
<% end %>
</div>
and here my controller :
def index
#clubs = Club.all
#search = params[:search][:value]
end
I think that the problem is about how I catch my variable :value but I don't know how to fix it.
Thanks
A quick thought is that text_field has a :value parameter to set the default value.
Try changing :value to something else
<%= form_for :search, url: {} do |f| %>
<%= f.text_field :keywords, :value => params[:keywords], :class => 'form-control input-sm', :placeholder => 'Search...' %>
<%= button_tag '<i class="fa fa-search"></i>'.html_safe, :id => 'search-btn', :class => 'btn' %>
<% end %>
Then
#search = params[:search][:keywords]

Regarding rails testing with Capybara &Rspec ...capybara element not found

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)