How to Override Form Heading or Title in ActiveScaffold - ruby-on-rails

Is there an easy way to override the title on an ActiveScaffold page? I know I can customize the name of the model part, but what if I want to say "Let's make a new widget, shall we?" instead of "Create Widget"? It seems overkill to override the whole template or partial just to get a new heading.
The ActiveScaffold default _create_form_html.erb is
<% form_action ||= :create %>
<%= render :partial => "base_form", :locals => {:xhr => xhr ||= nil,
:form_action => form_action,
:method => method ||= :post,
:cancel_link => cancel_link ||= true,
:headline => headline ||= active_scaffold_config.... } %>
Is there a way to set that headline variable from inside the controller?

Inside the scaffold config write:
conf.list.label = 'The list label'
conf.create.label = 'Let's make a new widget, shall we?'

Related

to get data from a method in controller and display it in view

I have written a method to count the vote for particular section and to display it in view file for section. But when I call this method count is not incremented. Should i make any changes in following method
code for voteme method in controller is as:
def voteme(num)
#section = Section.find(params[:id])
#section.vote += num
end
and code in view file is
<%= link_to "up", :url => voteme_section_path(1), :html => { :method => :post }%>
also can anyone suggest me the code to display updated count value.I have a vote field in section model.
In your view file
<%= link_to "up", voteme_section_path(1), :method => :post %>
But I have a question, you are voting up against a section. So why you are passing 1 to it. you should pass the section object if you would have stored it in #section variable. So its better you can modify the link as
<%= link_to "up", voteme_section_path(#section), :method => :post %>
In your route file I guess you need to do some thing like this
resources :sections do
member do
post 'voteme'
end
end
And in the sections controller, 'voteme' action
def voteme
#section = Section.find_by_id(params[:id])
unless #section.blank?
#section.update_column('vote', #section.vote + 1)
else
flash[:notice] = 'Sorry something goes wrong'
end
redirect_to your-path-that-you want-to-show.
end
Try adding #section.save after you change the value.
This logic should also be in the model, rather than in the controller. The controller should only pass things back and forth between the model and the view.
def voteme(num)
#section = Section.find(params[:id])
#section.update_attribute('vote', #section.vote+1)
end

Missing template error on when background processing

I am using background processing with the resque gem and bioruby, and the processing is correctly functioning. I am however getting a "missing template" error. It seems the action is trying to load a template.
Missing template cosmics/start_batch, application/start_batch with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
I do not want a template loaded: the background process retrieves data from an external source and then updates a database table (this is all working).
The code is triggered by a button:
<%= link_to 'Process', start_batch_path, :class =>"btn btn-primary" %>
config/routes.rb
match '/cosmics/start_batch', :to => 'cosmics#start_batch', :as => 'start_batch'
resources :batches do
resources :batch_details
end
cosmics_controller.rb
def start_batch
#batch = Batch.create!(:status => 'created',:status_timestamp => Time.now)
#cosmics = Cosmic.find(:all, :conditions => {:selected => true}).each do |cosmic|
#batch_detail = BatchDetail.create!(:batch_id => #batch.id, :cosmic_mut_id => cosmic.cosmic_mut_id)
#batch_detail.save
cosmic.selected = false
cosmic.save
end
Resque.enqueue(UcscQuery,#batch.id)
end
workers/ucsc_query.rb (Reqsue worker class)
class UcscQuery
require 'bio-ucsc'
include Bio
#queue = :ucsc_queue
def self.perform(batch_id)
Ucsc::Hg19.connect
#batch_detail = BatchDetail.find(:all, :conditions => {:batch_id => batch_id}).each do |batch_detail|
ucsc_cosmic = Ucsc::Hg19::Cosmic.find_by_name(batch_detail.cosmic_mut_id)
if ucsc_cosmic
batch_detail.bin = ucsc_cosmic.bin
batch_detail.chrom = ucsc_cosmic.chrom
batch_detail.chrom_start = ucsc_cosmic.chromStart
batch_detail.chrom_end = ucsc_cosmic.chromEnd
batch_detail.status = 'processed'
batch_detail.save
end
end
Batch.update(batch_id, :status => 'located')
end
end
How can I prevent Rails from trying to load a cosmics/start_batch template? Any refactoring tips would also be appreciated.
If there is no render nor redirect instruction in a controller method, Rails looks for a view with the same name of the method. To change this behavior, add render :nothing => true at the end of the start_batch method.
With this, when the user will click on the Process link, it will render a blank page. That's certainly not what you want. You can use :remote => true option in the link_to so the user will stay on the current page:
<%= link_to 'Process', start_batch_path, {:remote => true}, {id: 'process_btn', :class => "btn btn-primary"} %>
Finally, use javascript to show the user that "something" happened when he clicked on the button. Example:
$('#process_btn').on('click', function() { alert('Batch process started'); };
You can simply type:
render :nothing => true
in your action. I would also advice changing your link to remote
<%= link_to 'Process', start_batch_path, :class =>"btn btn-primary", :remote => true %>
Otherwise you'll see blank page after clicking on it.

How to make a custom route in Rails? By custom I mean one that reacts to params

So essentially I've setup a route to match "products/:product", which seems to respond to a page like baseurl/products/toaster and displays the toaster product. My problem is I can't seem to use link_to to generate this path, and by that I mean I don't know how. Any help on this?
There are several solutions on this one :
<%= link_to 'Toaster', { :controller => 'products', :action => 'whatever', :product => 'toaster' } %>
But it's not really Rails Way, for that you need to add :as => :product at the end of your route. This will create the product_path helper that can be used this way :
<%= link_to 'Toaster', product_path(:product => 'toaster') %>
Within your routes file you can do something like:
match "products/:product" => "products#show", :as => :product
Where the controller is ProductsController and the view is show
within the Products controller your have
def show
#product = Hub.find_by_name(params[:product])
respond_to do |format|
format.html # show.html.erb
end
end
Where whatever is in the products/:product section will be available via params.
Then, since we used :as in your routes you can do this with link_to:
<%= link_to product(#product) %>
Where #product is an instance of a product or a string. This is just an example and the param can be anything you want, the same goes for controller/action. For more info you should check out this.
Hope this helps!

Custom Settings Form in ActiveAdmin

I'm using rails-settings by Squeegy from https://github.com/Squeegy/rails-settings as well as Activeadmin. What I'm trying to accomplish is making a form in ActiveAdmin that I can let the site admin change the settings for the site, which take a command line syntax of:
Setting.foo = "bar"
Setting.site_title = "My Awesome Site!"
Setting.max_users = 35
I really don't think I've got too far, but I'm already stuck. I'm up to the point of having a custom ActiveAdmin form made:
ActiveAdmin.register_page "Settings" do
action_item do
link_to "View Site", "/"
end
content do
form do |f|
#Inputs for Settings
end
end
end
But I don't even know how to begin laying out the form to directly access the Settings model, or how to make a custom controller to handle the input. I suppose if I could get the input sent to a controller that I could make, I'd be just fine.
This is very simple to do with ActiveAdmin.
Lets say your settings class is Settings :
ActiveAdmin.register_page "Settings" do
content do
table :class => 'settings' do
thead do
th 'Setting'
th 'Value'
th ''
end
Settings.all.each do |key, val|
tr do
td strong key
td val
td do
link_to "delete", admin_settings_delete_path( :key => key ), :method => :post
end
end
end
tr do
form :action => admin_settings_create_path, :method => :post do
td do
input :name => 'key'
end
td do
input :name => 'val'
end
td do
input :type => 'submit', :value => 'Add'
end
end
end
end
end
page_action :create, :method => :post do
Settings[params[:key]] = params[:val]
redirect_to :back, :notice => "#{params[:key]} added"
end
page_action :delete, :method => :post do
Settings.destroy params[:key]
redirect_to :back, :notice => "#{params[:key]} deleted"
end
end
Of course you'll need to add some CSS and maybe some validations but you have your settings page.
Edit:
Note that I wrote this for rails-settings-cached, not rails-settings, but my quick search led here so I guess this could still help someone.
I don't think you want your site's form to directly change the settings in ActiveAdmin, I would ...
Create a new table, eg. adminsettings and add fields for each of the settings you want to store for instance site_title, alternatively you could use each row for a setting which means you can add new settings in the future without changing the database
Put together a form in Activeadmin to maintain your settings
Add some functions to your model to grab the settings so you can do something like ..
Setting.site_title = Adminsetting.getsitetitle
You could be clever with your model method and use the method_missing facility so you need the least amount of code to get a setting ...
class << self
def method_missing(method, *args, &block)
setting = Adminsetting.where(:code => method.to_s).first
if setting
return setting.content
else
return super(method, *args, &block)
end
end
Perhaps you could package this into a Gem as it could be a useful thing for others.

RoR: Link_to_remote - updating text field

I'm trying to use rails to change a textfield's value with a link_to_remote
<%= link_to_remote address.street, :url => {:controller => 'incidents', :action=>'street_selected', :update => "street.value" } %>
Street is the id of the textfield
my controller function renders text, but the textfield value isn't changed. How do i get this to work?
You could either remove and replace the text field or just update the value. Updating the value itself probably much simpler. The following assumes you haven't switched out Prototype for jQuery or another JS toolset.
In the view:
<%= link_to_remote address.street, :url => {:controller => 'incidents',
:action=>'street_selected'} %>
In the controller
def street_selected
...
code that gets new value
...
respond_to |format| do
format.js { render :update do |page|
page <<"$('textfield').value = new_value
end
}
end
P.S. You might want to pass some parameters in that remote link to allow for dynamic processing. Otherwise there's no point in doing this with AJAX.

Resources