I started to use the best_in_place gem, until now all worked fine. But i have my problems to get the gem work in an table:
<% #ebms.each do |ebm|%>
<tr>
....
<td><%= best_in_place ebm, :extra %></td>
First strange thing is that the empty column :extra is shown with -- how you can see here:
Next thing, i cant update extra, it always displays the same --, as well when i reload!
Here is an question with the same issue: Using the best_in_place gem within a table
Related
I created a Rails application to create questions according to the category.
When editing my question I would like to be able to choose the order in which the question appears
show image bdd
the column sort is identical to question_id but me question is a nested of category so i want the sort for the sub-ressource
And when I drag and drop an element the column sort don't change so when I refrech the order is the same
The 'rails_sortable' gem might be the solution you are looking for. It is a wrapper gem for jquery sortable library(https://jqueryui.com/sortable/).
It will allow you to reorder question by drag and drop.
Add the following gems to your gem file and run bundle install
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'rails_sortable'
Add the following require statements to application.js file
//= require jquery
//= require jquery_ujs
//= require jquery-ui/widgets/sortable
//= require rails_sortable
Now you need to add sort a column to your Question model(I am assuming here your model name is Question) say 'sort' or 'position'. This will be the position/order of a particular question
And then add the following to your Question.rb(model) file
class Question < ApplicationRecord
include RailsSortable::Model
set_sortable :sort # Indicate a sort column
# If you do NOT want timestamps to be updated on sorting, use the
following option.
# set_sortable :sort, without_updating_timestamps: true
end
While fetching the question data in the controller, order it based on the sort column
class QuestionsController < ApplicationController
def index
#questions = Question.order(:sort).all
end
end
And in the view,
<table>
<tbody class="sortable"> <!-- sortable target -->
<% #questions.each_with_sortable_id do |question, sortable_id| %>
<tr id="<%= sortable_id %>"> <!-- Needs id tag on sorting
elements -->
<td><%= question.id %></td>
<td><%= question.content %></td>
<td><%= question.category_id %></td>
<td><%= question.sort %></td>
</tr>
<% end %>
</tbody>
</table>
And finally in the javascript file,
$(function() {
$('.sortable').railsSortable();
});
rails_sortable github page: https://github.com/itmammoth/rails_sortable
I have a Rails 4.0 APP using PostgreSQL on Heroku. I am trying to display a table that is my XLog or transaction log, showing the last five entries in reverse order by updated_at timestamp. This works correctly on my local system. When I push it to Heroku, it sorts incorrectly.
I have checked the Heroku database definitions and the column is correctly listed as a timestamp. I have cloned the Heroku code back to my machine and verified that it is the same as what I pushed. At this point, I don't know why it doesn't work on Heroku when it works locally. And advice would be appreciated.
FWIW, the remote database and local database do not have the same data.
The code is: (Last line of log_sort was added to act as a breakpoint that would still pass the correct result.)
def self.last_objects object, count
logs = XLog.where(object: object).last(count)
log_sort = logs.sort_by{|log| log.updated_at}.reverse
log_sort
end
During execution to the breakpoint, you can see the variables passed:
This is the local result with the correct sort:
This is the Heroku result with the incorrect sort:
This is the Heroku PostgreSQL table definition for updated_at:
EDIT: View:
<% xlog = xlog_last(car.stock_number, 5) %>
...
<% xlog.each do |log| %>
<tr>
<td><%= log.associate %></td>
<td><%= log.proxy %></td>
<td><%= log.action %></td>
<td><%= log.status %></td>
<td><%= log.message %></td>
<td><%= log.value %></td>
<td><%= log.updated_at %></td>
</tr>
<% end %>
Helper:
def xlog_last(object, count)
XLog.last_objects object, count
end
EDIT:
I modified the sort_by to use an order method as follows. The results did not change at all. The same sorting error occurred and the exact same data was displayed in the exact same way:
New code:
def self.last_objects object, count
logs = XLog.where(object: object).order(updated_at: :desc).first(count)
end
I believe you need to use order to influence the actual sql query. Right now you are using sort_by which is sorting the data after you read in from the database. The order in the db is based on how it was inserted and could be different from heroku and your local system. When you export from heroku, and then import it, the ordering of the tables probably changes too.
A gem was incorrectly sorting the view. Correcting that issue fixed the problem. Still unsure why it didn't show in test except possibly for the data differences.
I have a ruby on rails application that recently started giving me issues.
I believe there may be a weird bug/feature in the way rails is pluralizing model names for the database.
For example,
I have a model called DiagExerciceWeekFive. The table in the database is called diag_exercice_week_fives. The pluralization works correctly here.
I think there may be a problem in the way rails is attempting to "de-pluralize" the table into the respective objects.
When I try to load up a simple form that displays all of my diagweekfives, I get this error:
uninitialized constant Diag::DiagExerciceWeekFife
Not once have I used that name in my application.
Here's the relevant bit of code that is throwing the error:
<% ExerciceWeekFive.all.each do |exercice| %>
<tr class="success">
<td><%= check_box_tag :exercices_week_five_ids, exercice.id, #diag.exercices_week_fives.include?(exercice), :name => 'diag[exercices_week_five_ids][]' %></td>
<td><%= exercice.number %></td>
<td><%= exercice.description %></td>
</tr>
The exception is thrown on the first <td> within the <tr>
Has anyone run into this before? I know little about rails, but I am trying to maintain some legacy code.
Thanks.
I have been building a practice application for rails. It's an simple blog application with only articles model, and tagging system which is done using Postgresql Arrays. Everything is fine except, I couldn't get the pages for specific tags to working.
The Activerecord query to fetch the posts matching the given tags is working properly in rails console, but the same fails when tried with View. I have verified and the params are passing through, please help.. I don't receive any error messages, from what I could understand the rails passes the ActiveRecord::Relation::ActiveRecord_Relation_Article as a result of query to the view, but the view file fails to display it list by list...
Below are the relevant files,
routes.rb
get 'tags/:tag', to: 'articles#tagview', as: :tag
articles_controller.rb
def tagview
#articles = Article.where("'params[:tag]' = ANY (tags)")
end
tagview.html.erb
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.content %></td>
<td><%= article.tags.join(" ") %></td>
</tr>
<% end %>
The resulting page is just blank. whereas the above query works in Rails console.
The problem
Your original code sends a literal 'params[:tag]' string to the database, and it would have no idea what to do with it. The resulting query looks something like:
SELECT * FROM articles WHERE ('params[:tag]' = ANY (tags));
This only returns articles that has actually been tagged with the literal "params[:tag]" string.
The solution
I guess you intend for params[:tag] to be replaced with the actual tag value coming in from params. To do this, you'd need to do something like:
Article.where(['? = ANY (tags)', params[:tag]])
which is going to result in something like this being sent to the database (fx when params[:tag] is set to "foo"):
SELECT * FROM articles WHERE ('foo' = ANY (tags));
which is much more likely to be what you want.
Further reading
If you want to read more about how to construct queries with Active Record, the section about Conditions in the Active Record Query Interface guide is a good place to start.
You probably won't find anything in the official documentation that talks about using PostgreSQL arrays as that is not a general database feature, though.
I'm aware that variants of this question have been asked before, but I do believe my question is different ;-)
I have 2 Ruby on Rails classes (with assoc. controllers, models, etc). These are Devices and Messages.
In the Devices index.html view, I need to make reference to the newest Message record.
<table>
<tr>
<th>Device Name</th>
<th>Checked</th>
</tr>
<% newestMessage = Message.find(:all, :order => "updated_at DESC", :limit => 1) %>
<% #devices.each do |device| %>
<tr>
<td><%= device.name %></td>
<td><%= newestMessage.updated_at %></td>
</tr>
<% end %>
</table>
(I believe I should move the logic to a helper, but for the time being I'm keeping it as simple as possible while I get it working)
When I access this blahblah.com/devices, I get...
undefined method `updated_at' for #<Array:0x103f36c00>
Due to the line,
<td><%= newestMessage.updated_at %></td>
Now, I've seen questions like this that suggest to add the equivalent of the following to messages_controller.rb,
attr_accessor :updated_at
However, I've tried this and it doesn't help. Also, I don't think this should be necessary anyway as 'updated_at' is derived from the database, built with scaffold, etc.
If I just print 'newestMessage' it seems to have a sensible value.
My question is, how should I access fields within newestMessage, which are of class Message from the Device index.html.erb?
Thanks in advance.
Try newestMessage = Message.last then newestMessage.updated_at