Change the order of an nested content - ruby-on-rails

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

Related

Rails error in inline edit using gem 'best_in_place'

I am trying use gem 'best_in_place' in table edit, but having error while using it
in controller only index method for display all users details from db.
Error: undefined method name' for "Rohit":String`
Code used:
<% #user_record.each do |record| %>
<tr>
<td><%= best_in_place record.name, :name %></td>
</tr>
<% end %>
controller Code:
def index
#user_record = MUser.search(params[:name],params[:gender])
end
def create
end
as I see from the docs the first argument you pass to the method best_in_place should be an object record and the second - a field you need to edit with this gem, in your case it is name.
So it will get provided field name from the record.
And since you've called name by yourself, it returns the actual value of the name instead of the whole object record and then tries to get name from that value which results in this error.
So you need to change that line to <td><%= best_in_place record, :name %></td>.

Ruby on Rails Custom Method/Task

I'm new to Ruby on Rails. I'm building out an app that does custom operations on defined nodes (the object). Here's my table:
<% #nodes.each do |node| %>
<tr>
<td><%= node.name %></td>
<td><%= link_to 'Bootstrap', node_path(node),
method: :bootstrap %>
</td>
</tr>
<% end %>
"Bootstrap" is the custom code that I want to run. I'm defined a custom method within my Controller:
class NodesController < ApplicationController
def bootstrap
......
end
........
end
How do I tie in my custom code to my Rails app so that when a user clicks on the link the code will be run against that object?
In config/routes.rb, you probably have:
resources :nodes
Change this to:
resources :nodes do
get 'bootstrap', on: :member
end
Then, run rake routes to see that you now have a new route method, bootstrap_node_path(node), and will link to /nodes/:id/bootstrap.
I recommend this over the other approach as it keeps your route details together, but that's just my personal opinion. I usually resort to custom routes as a last resort.

Rails stock ticker dashboard widget proof of concept

I’m new to rails and trying to expand beyond just the standard scaffolding. I would like to build a dashboard that isn’t based on just one model but a collection of models (partials?) to view weather, the stock market, etc. Some of this data will be pulled from queries from models. Other elements will be hard coded, like the stock ticker widget which I thought I would begin with.
I began by creating a dashboard.rb file in the lib folder. I used the following code
class Dashboard
def self.tickertable
yahoo_client = YahooFinance::Client.new
data = yahoo_client.quotes(["BVSP", "NATU3.SA", "USDJPY=X"], [:ask, :bid, :last_trade_date])
#ask = data.ask
end
end
Next, I added the necessary code to the application .rb file to load the lib folder
config.autoload_paths += %W(#{config.root}/lib)
I then added a _tickertable partial in a newly created views/shared folder.
<table>
<thead>
</thead>
<tbody>
<% Dashboard.tickertable.each do |ticker| %>
<tr>
<td><%= ticker.name %></td>
<td><%= ticker.symbol %></td>
<td><%= ticker.last_trade_price %></td>
</tr>
<% end %>
</tbody>
</table>
I put this partial to an already existing view which when it loads it gives the following error:
undefined method `ask' for #<Array:0x6663330>
on the "<% Dashboard.tickertable.each do |ticker| %>" line.
What key element am I missing? I feel like I am close but clearly something is missing.

Rails best_place_gem for use in table

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

on_the_spot gem not working

I have just installed the "on_the_spot" gem following the github instructions.
And I'm trying to create a in-place edit for my index action. When I get the mouse over the text that should be editable, nothing happens. Only the background color changes.
This is the relevant code from my index view
<% #part_types.each do |part_type| %>
<tr>
<td><%= on_the_spot_edit part_type, :title %></td>
</tr>
<% end %>
From the controller:
class PartTypesController < ApplicationController
#on_the_spot for in place editing
can_edit_on_the_spot
#.. rest of the controller code
end
Added to the routes:
resources :part_types do
collection do
put :update_attribute_on_the_spot
end
end
nginx already restarted after these changes.
Thank you
Inside your Gemfile add the following:
gem "on_the_spot"
Run the installation task:
rails g on_the_spot:install
if u using rails 3.1 replace created files to app/assets/javascripts (two files jquery.jeditable.mini and on_the_spot)

Resources