on_the_spot gem not working - ruby-on-rails

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)

Related

Ruby On Rails file selector

I am developing my first Ruby On Rails project and I have the following question.
Is it possible to display a window with the contents of a folder so I can select a file? For example, images from the "app/assets/images" project folder.
I have searched in Google and rubygems.org but have not found anything.
Respectfully,
Jorge Maldonado
You could write a controller method and associated view that scan over a folder and output the contents. Here's an example.
Controller:
def folder
#files = Dir.open(Rails.root.join('public', 'images'))
end
View:
<table>
<% #files.each do |file| %>
<tr><td><%= link_to file, file_path(file) %></td></tr>
<% end %>
</table>
Note that file_path is assumed to be some route defined in routes.rb which accepts a file name and allows the user to view/open/download that file.
Here's a gem that also gives similar functionality:
https://github.com/furkancelik/fcfinder

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.

Ruby On Rails Display Image

I am a newbie at it.I have just educated myself for 2 days. And a have a problem.
Example: I have a table , called as tblData, includes 2 columns: id, img_link. img_link contains link to an image.
I want to show all of them in this table(id and image, not image link) into a html file.
So, exactly what I need do?
If you have an image link that you can pull from your database, you can do something like this:
Assuming you have an object assigned and everything's set up:
In your controller for tbl_data (e.g. app/controllers/tbl_data_controller.rb):
class TblDataController < ApplicationController
def your_action
#tbl_data_item = TblData.first
end
end
(The code above is just an example, you should substitute for whatever code/query you wish to run)
In your view template, you can render an image from a link using the following Rails view helper:
<%= image_tag(#tbl_data_item.img_link) %>
This would output the following HTML:
<img src="/path/to/image/from/img_link" />
There's a lot more info on this helper on the Rails api docs. The Rails Guides has some awesome info on getting things set up and running as well. Hope this helped!
UPDATE:
To give you a better example with clearer steps, you would do something like the following:
Set up your routes (app/config/routes.rb):
Rails.application.routes.draw do
resources :tbl_data
end
Create your model, used to communicate with its respective database table (app/models/tbl_data.rb):
class TblData < ActiveRecord::Base
# your model-specific code goes here - validations, scopes, etc.
end
Create the controller, which responds to when a user hits a certain route in your app (app/controllers/tbl_data_controller.rb):
class TblDataController < ApplicationController
def your_action
#tbl_data_items = TblData.all
end
end
Create the view template, that will be rendered for your user(app/views/tbl_data/your_action.rb):
<table>
<% #tbl_data_items.each do |item| %>
<tr>
<td><%= item.id %></td>
<td><%= image_tag(item.img_link) %></td>
</tr>
<% end %>
</table>
The above would show a table with each record in the #tbl_data_items as a row, with 2 columns, one with the id, and one with the actual image for that item.
Create a folder in public called images, put the desired image in that folder.
Put <%=image_tag 'whatevertheimagefilenameis.png', class: 'example-class', id: 'example-id'%> in the html.

Create an audit trail using paper_trail gem

I'm pretty new to rails, and brand new to using the paper_trail gem.
I would like to show a list of each change made to the location or tag number of my assets model. I have installed paper_trail and it seems to be working correctly. In my view I have this:
<% #asset.versions.order('created_at DESC').each do |version| %>
On <%= version.created_at %> <%= version.whodunnit %> updated this asset.<br>
<ul>
<li>Asset relocated from "<%= version.last.locaton %>" to "<%= version.location %> "</li>
<li>User changed from "<%= version.last.tag_number %>" to "<%= version.tag_number %> "</li>
</ul>
<% end %>
This gives me the correct information for when the change was made and who made it, but I cannot figure out to display the lines that say (for instance): Asset relocated from "223" to "258 or Tag Number changed from "1173" to "1175".
Ideally I would only show information for things that have changed. So if the location changed but the tag number did not, I would show only the location line.
I don't really even know where to start. Any help would be appreciated. Thanks!
From The PaperTrail Documentation:
PaperTrail has an optional table column that can be used when creating versions called object_changes. This will store ONLY the changes made between versions. Which will allow you to just display the changes between each version. It works like this.
>> widget = Widget.create :name => 'Bob'
>> widget.versions.last.changeset # {'name' => [nil, 'Bob']}
>> widget.update_attributes :name => 'Robert'
>> widget.versions.last.changeset # {'name' => ['Bob', 'Robert']}
>> widget.destroy
>> widget.versions.last.changeset # {}
If you recently added paper_trail to your model then you can run a migration to add this column. This will track future changes.
class AddObjectChangesColumnToVersions < ActiveRecord::Migration
def self.up
add_column :versions, :object_changes, :text
end
def self.down
remove_column :versions, :object_changes
end
end
I recently had a need to get the changes, however, we have years of version data for 20+ models and adding that migration would only help in the future and not give me all of the data changes in the past.
I wound up creating a patch for PaperTrail that added methods like diff_previous, diff_next, diff_live which would give me the difference between versions. It was based off the idea of this patch of paper_trail(https://github.com/jeremyw/paper_trail/blob/master/lib/paper_trail/has_paper_trail.rb#L151-L156) but I implemented it quite differently. If you want more details I will see if I can open source that patch.

ruby on rails directory browser

I'm trying to write a simple RoR app that lists all directories in a specific path then allows me to click that file or directory to either open the file or open the directory. I'm using the following code which works to list the directories but I can't figure out how to open the file or directory from here. When clicking the directory I get a "No route matches [GET] "/selected file" error.
controller
#file = Dir.foreach ("/specifiedpath/")
view
<% #file.each do |file| %>
<tr>
<td><%= link_to file, file %></td>
</tr>
<% end %>
Any help would be appreciated!
You need something in your routes.rb that looks like this:
get '/browse/*path' => 'browse#show', as: :browse
In your browse_controller, you'll want a show action that does something like:
class BrowseController < ApplicationController
def show
#path = params[:path] || '/some/default'
#files = Dir.foreach(path)
end
Finally, in your browse/show.html.haml (I prefer HAML, sorry) view, something like:
- #files.each do |file|
%tr
%td= link_to file, browse_path(File.join(#path, file))
(Pardon me if it all doesn't work outright—haven't time to fully test a fleshed out solution right now—but I can delve into it a little more if you have trouble getting it to work yourself.)

Resources