Installing authlogic 3.4.3 in Rails 4.1.7 - authlogic

Since yesterday I have been trying to install authlogic in a project. As I can see, all the examples and the documentation are oriented to older versions of both rails and authlogic. However, I tried to follow :
the example in the documentation
the railscast
other websites examples like: http://www.logansbailey.com/2010/10/06/how-to-setup-authlogic-in-rails-3/ (for me, the most useful tutorial until now)
I don't get authlogic working properly.
I installed the gem trough the gemfile and the bundle install order.
I generated a model user and ran the migration
$ rails generate scaffold user username:string email:string crypted_password:string password_salt:string persistence_token:string
$ rake db:migrate
I add the following line to the model user.rb:
acts_as_authentic
then, I put a link to register a new user: link_to "Register", new_user_path
I edit /app/views/users/index.html.erb to look like:
<h1>Listing users</h1>
<table>
<tr>
<th>Username</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
and app/views/users/_form.html.erb to look like:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Now, when I try to register an user, I get two errors:
-Password is too short (minimum is 4 characters)
-Password confirmation is too short (minimum is 4 characters)
I have put longer passwords but it doesn't work.
Any help?
Thank you

Just based on the error messages without seeing the users controller, it appears that password and password_confirmation are not being passed to the user model. This may be a Rails 4 preventing mass assignment issue. To allow this, in the create method in the users controller, you can say:
#user = User.new(user_params)
and then define the user_params method:
def user_params
params.require(:user).permit(:password, :password_confirmation, :name, :email)
end

Related

Working with node and ejs. Why might <%= no be recognised inside quotes "", no colour change

The issue is on the 3rd row below. With in the href""'s, there is text and an a js script syntax, <%=. for some reason, the <%= is not being recognised as a script insert.
Any idea's on why this is happening and how to fix? I'm using node.js, Express, MongoDB, Mongoose
<% if(users.length > 0) { %>
<% users.forEach(function(user){ %>
<a href="/getUsers/<%= user._id %>">
<tr>
<td><%= user.userId %></td>
<td><%= user.userName %></td>
<td><%= user.userOperator %></td>
<td><%= user.userEmail %></td>
<td><%= user.userMobile %></td>
<td><%= user.userAdminYN %></td>
</tr>
</a>
<% }) %>
<% }else{ %>
I tried adding the script syntax in, and was expecting to see it change colour as it does elsewhere in the core.
Use a template string:
<a href="<%= `/getUsers/${user._id}` %>">

SilverStripe (3.6.2) Search returning assets folder contents

I haven't enabled Search on SilverStripe before, but it seems pretty easy. I've followed steps from 2 other projects (although they are 3.5 version projects but not sure that makes a difference or not) that have search enabled as well as the tutorial offered on SilverStripe's site, and for some reason, I'm getting asset folder items (i.e. images) in my search results. It only seems to happen if I click to search and nothing has been entered into the search field.
There should be no asset items returned at any time for search, and if there is no search query, then there should be a message saying nothing was entered or something. I noticed that using the default $SearchForm setup provided by the basic install gives me the desired results, but not for the form I'm using (which does work on 2 other SilverStripe sites--I checked and confirmed).
I'm not sure what I'm missing? I feel like everything is done correctly, and I would like to use the setup I have now to give me more styling ability:
From _config.php:
FulltextSearchable::enable();
From my Header.ss file:
<!-- SEARCH BAR -->
<form class="navbar-form navbar-left nav-right-left search-form" id="SearchForm_SearchForm" action="/home/SearchForm" method="get" enctype="application/x-www-form-urlencoded">
<fieldset style="font-size: 0;">
<div class="field text nolabel search-holder">
<input name="Search" placeholder="Search" class="form-control search-field text nolabel active search-box" />
</div>
<div class="ja-search-box">
<button class="icon search-button smiths-search-btn" type="submit"><i class="glyphicon glyphicon-search pull-right"></i></button>
</div>
</fieldset>
</form>
The Search Results page:
<div class="main" role="main">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="Content" class="searchResults">
<h1 class="brand-red">$Title</h1>
<% if $Query %>
<p class="searchQuery">You searched for "{$Query}"</p>
<% end_if %>
<% if $Results %>
<ul id="SearchResults">
<% loop $Results %>
<li>
<h4>
<a href="$Link">
<% if $MenuTitle %>
$MenuTitle
<% else %>
$Title
<% end_if %>
</a>
</h4>
<% if $Content %>
<p>$Content.LimitWordCountXML</p>
<% end_if %>
<a class="readMoreLink" href="$Link" title="Read more about "{$Title}"">Read more about "{$Title}"...</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>Sorry, your search query did not return any results.</p>
<% end_if %>
<% if $Results.MoreThanOnePage %>
<div id="PageNumbers">
<div class="pagination">
<% if $Results.NotFirstPage %>
<a class="prev" href="$Results.PrevLink" title="View the previous page">←</a>
<% end_if %>
<span>
<% loop $Results.Pages %>
<% if $CurrentBool %>
$PageNum
<% else %>
$PageNum
<% end_if %>
<% end_loop %>
</span>
<% if $Results.NotLastPage %>
<a class="next" href="$Results.NextLink" title="View the next page">→</a>
<% end_if %>
</div>
<p>Page $Results.CurrentPage of $Results.TotalPages</p>
</div>
<% end_if %>
</div>
</div>
</div>
</div>
</div>
By default, the full text search will search array('SiteTree', 'File')
http://api.silverstripe.org/en/3.1/class-FulltextSearchable.html
I would try changing your FulltextSearchable::enable(); line to FulltextSearchable::enable(array('SiteTree'));
I haven't tried this before and am not sure if it will work.

Uncaught ReferenceError: JST is not defined on Sails 0.11

while using Sails I get this error Uncaught ReferenceError: JST is not defined on the chrome console everytime this line of code:
JST['SailsApp/assets/templates/addUser.ejs']( obj )
The content of the addUser.ejs file is:
<tr data-id="<%= user.id %>" data-model="user">
<% if (user.online) { %>
<td><span class="fa fa-circle fa-lg fa-circle-green"></span></td>
<% } else { %>
<td><span class="fa fa-circle fa-lg fa-circle-red"></span></td>
<% } %>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.title %></td>
<td><%= user.email %></td>
<% if (user.admin) { %>
<td> <span class="glyphicon glyphicon-king" > </span></td>
<% } else { %>
<td> <span class="glyphicon glyphicon-user" > </span></td>
<% } %>
<td>Show </td>
<td>Edit </td>
<td>
<form action="/user/destroy/<%= user.id %>" method="POST">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" class="btn btn-sm btn-danger" value="Delete"/>
<input type="hidden" name="_csrf" value="<%= _csrf%>" />
</form>
</td>
</tr>
The compiled .jst file is not appearing in .tmp/public folder, and if I run sudo grunt jst manually then it's created but the chrome console continues giving the same error.
Any help would be much appreciated.
Thanks in advance!
I had the same problem recently. Under 'tasks/register/compileAssets.js', try moving jst:dev below copy:dev.
module.exports = function (grunt) {
grunt.registerTask('compileAssets', [
'clean:dev',
'less:dev',
'copy:dev',
'jst:dev',
'coffee:dev'
]);
};
It seems that the jst:dev task is looking for the template files in the .tmp/ dir, but they aren't moved from assets/ until the copy task executes. You'll see this if you run grunt and get an error like:
Running "jst:dev" (jst) task
>> Destination not written because compiled files were empty.

Having trouble correctly saving a nested form that has file_field

Using paperclip and ruby-mp3info to upload and read file info, I have a form that creates a playlist and its children, tracks:
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="table">
<thead>
<tr>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
The problem with uploading a file is that I won't have it until I click on the Save button. So I read the info from within my controller. Within the update method of my Playlist controller, I try to create a new track by the following code:
def update
track = #playlist.tracks.new()
Mp3Info.open(playlist_params["tracks_attributes"]["0"]["audio"].path.to_s) do |info|
track.audio_file_name = info.tag.title
track.artist = info.tag.artist
track.album = info.tag.album
# tracknum = info.tag.tracknum.to_s
end
respond_to do |format|
if #playlist.update(playlist_params)
format.html { redirect_to #playlist, notice: 'Playlist was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
The problem is that the code in my update method will create TWO instances of tracks when I only want one. How do I create only the instance that I am creating from within my controller and ignore the new track that is being created from within the form?
Are there alternative ways to do this?
You are getting two records in tracks table because one you are creating from form (with track_attributes) and other you are creating in update action.
If you just need to update few properties of the track to be saved then do it on the track coming in params[:playlist][:tracks_attributes].
Change the update action as below:
def update
Mp3Info.open(playlist_params["tracks_attributes"]["0"]["audio"].path.to_s) do |info|
params[:playlist][:tracks_attributes]["0"][:audio_file_name] = info.tag.title
params[:playlist][:tracks_attributes]["0"][:artist] = info.tag.artist
params[:playlist][:tracks_attributes]["0"][:album] = info.tag.album
# tracknum = info.tag.tracknum.to_s
end
respond_to do |format|
if #playlist.update(playlist_params)
format.html { redirect_to #playlist, notice: 'Playlist was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
This will create only one associated Track record for a Playlist in tracks table.

Search result displays on wrong page

I am having a bit off difficulty implementing the ransack gem I have a pages controller with an index action and post controller also with an index action. However when I perform a search in the index action of the pages controller (pages#index) the results are rendered in the index action of the post controller(posts#index). Does this mean i can only search from within the views of a particular resource or am I making a mistake ?
pages#index
def index
#q = Post.search(params[:q])
#posts = #q.result(distinct: true)
end
pages#index
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
<%= search_form_for #q,:url=>pages_path do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
Modify the code in the search form like this. It will work now.

Resources