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.
Related
I was just trying out the Elastic Search in my Rails app replacing the existing search function. Everything worked nicely but I am getting http://localhost:3000/contents/video/%23%3CElasticsearch::Model::Response::Result:0x007fe24e118f40%3E url.
in content.rb
def to_param
"#{id}/#{title.parameterize}.html"
#"#{id}-#{title.downcase.slice(0..30).gsub(/[^a-z0-9]+/i, '-')}.html"
end
and in search action
def search
if params[:q].nil?
#indexs = []
else
#indexs = Content.search params[:q]
end
and in views
<% #indexs.each do |f|%>
<%= link_to((truncate f.title, length: 60), {:controller => "contents", :action => "weblinks", :id => f.to_param}, target: "_blank") %>
<% end %>
It works fine for the default listing page, but its URL generating error in search result page. Please help,and also how to replace the {:controller => "contents", :action => "weblinks", :id => f.to_param} with weblinks_path(:format) routes
I assume you use elasticsearch gem, if so you should use records to get ActiveRecord models #indexs.records
I'm having a problem with submitting a simple form, and I'm not entirely sure what the deal is. The problem, I think, is that I'm trying to submit a form from a User's profile page to another model (ItemShare). Instead of submitting the form via post, it's trying to make a GET request. When I've changed the ItemSharesController to allow for this, it puts ItemShare#index into my modal form, even though I've specified in a billion places that I want it to post. POST!
In my routes.rb:
match '/item_shares' => 'item_shares#create', :via => :post
resources :item_shares, :except => :create
The form:
#share-list-button-dialog.modal.hide.fade{:role => "dialog"}
.modal-dialog
%h3 Share List
.modal-body
=form_for(ItemShare.new, :method => :post, :url => {:action => "create"}) do |f|
=f.hidden_field(:item_id, :value => item.token)
=f.hidden_field(:owner_id, :value =>user.id)
=f.label :shared_user_email, "Your collaborator's email:"
=f.text_field :shared_user_email, :value => "collaborator#example.com"
=f.submit "Share"
ItemSharesController:
class ItemSharesController < ApplicationController
def new
#item_share = ItemShare.new
end
def create
#item_share = ItemShare.new(params[:item_share])
respond_to do |format|
if #item_share.save
format.html {redirect_to user_path(current_user.id), :notice => "List shared successfully"}
else
flash.now[:alert] = "Could not share list."
end
end
end
end
And this is what the stack trace is showing:
Started GET "/item_shares/" for 127.0.0.1 at 2013-10-15 21:40:44 -0400
ActionController::RoutingError (No route matches [GET] "/item_shares"):
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
...
What's going on here??
UPDATE ... finally know what was up.
The form itself was not the problem, but the button I was using to trigger the modal form. I had:
%button{'data-toggle' => 'modal', 'href' => '../item_shares/', 'data-target' => '#share-list-button-dialog'}.
Changed it to this:
=button_to "Share", item_shares_path, "data-toggle" => "modal", "data-target" => "#share-list-button-dialog"
And now it's all working fine. Many thanks to Helios de Guerra for his help and patience. :)
From what you're describing, you should just be using the standard conventions.
Routes:
resources :item_shares
Form:
<%= form_for(ItemShare.new) do |f| %>
...
<% end %>
If it doesn't work, check the source of the rendered page. The form markup should include something like:
<form accept-charset="UTF-8" action="/item_shares" id="new_item_share" method="post">
Upon first glance, I'd say it has something to do with this =form_for(ItemShare.new
In my experience, Rails builds the path from the url in the form_for, which means that yours is going to be messed up with that. You might want to give this a try:
#users/new
def new
#item_share = ItemShare.new
end
#form
=form_for(#item_share
I'm not totally sure on it, but it's my hunch
I'm converting an application from rails 2 to rails 3 and could somebody please help me regarding this small bit of code. The link_to is not working , could some one point me how to use link_to instead of the link_to_remote in rails 3.1 properly?
Rails 2 code
<%= link_to_remote package_item.getId(),
:url => { :controller => 'cmn/popup',
:action => "show_popup",
:frame_url => admin_url(
:ctrl => controller,
:app_action => 'package.item.edit',
:id => package_item.getId().to_s,
:remote => true
),
:frame_width => '570px',
:frame_height => '355px'
}
%>
Rails 3.1 code
<%= link_to package_item.getId(),
:url => { :controller => 'cmn/popup',
:action => "show_popup",
:frame_url => admin_url(
:ctrl => controller,
:app_action => 'package.item.edit',
:id => package_item.getId().to_s
),
:frame_width => '570px',
:frame_height => '355px',
:remote => true
}
%>
I replace all .rjs file with .js.erb. This is the URL I'm getting in Rails 3:
3
This is in Rails 2:
2
my controller
def show_popup
#content_data = {}
#content_data.merge!(params)
render(:template => 'cmn/popup/show_popup', :nolayout => 1)
end
Please look at the syntax of link_to in Rails 3:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You have all your parameters in a :url hash, but you don't need to name it :url, just pass in the options in a hash, like this:
<%= link_to package_item.getId(),
{
:controller => 'cmn/popup',
:action => "show_popup",
:frame_url => admin_url(
:ctrl => controller,
:app_action => 'package.item.edit',
:id => package_item.getId().to_s
),
:frame_width => '570px',
:frame_height => '355px'
},
:remote => true
%>
Remember to get :remote out of the url hash.
Let me know if this works.
It is the first time I see a string defining a path as a :controller param.
I mean de :controller => 'cmn/popup'. This is new to me, and it feels strange.
Are you sure this works and the request is being received by the correct controller and action?
Another thing i think it can be tricky is the render call on the controller. Just call
render :layout => false
or maybe dont call anything at all.
If the template has the same name than the action and it is placed in a directory named like the controller, rails knows what template needs to be rendered and the extension (js/html/xml) based on the request type. Maybe render :template => .... forces to render an html template.
The :nolayout option i think it is invalid. Anyway, if the request is for a javascript file, it never renders layout.
currently i have an link as mentioned in below format
<%= link_to_if_authorized(l(:button_update), {:controller => 'issues', :action => 'edit', :id => #issue }, :onclick => 'showAndScrollTo("update", "notes"); return false;', :class => 'icon icon-edit', :accesskey => accesskey(:edit)) %>
i would like to apply toggle functionality on update could anyone please help me.
There are numerous possibilities to achieve a toggle button. You may choose to change the JavaScript function your button executes, or the function may toggle the Rails action to call. I personally would go for a Rails action that knows how to toggle itself:
def toggle_action
#model = Model.find param[:id]
if #model.active?
#model.active = false
render :js => "alert('Active!');"
else
#model.active = true
render :js => "alert('Inactive!');"
end
end
I want to delete my task ajax-style if some conditions are met. I do that with the help of link_to_remote. The thing is that link_to_remote wants to render a template and i dont want it to.
In my view ( _task_sth.html.erb):
<%= link_to_remote "Delete task", :url => {:controller => 'tasks', :action => 'delete_task', :task_pers_id => sorted_user_task.id}, :complete => "$('#{delete_task_domid}').hide();" %>
In my controller (tasks_controller.rb):
def delete_task
task_pers = TaskPersonalization.find(params[:task_pers_id])
horse_task = task_pers.task
task_pers.destroy
if horse_task.task_personalizations.empty?
horse_task.destroy
end
end
The task gets deleted but i get an error saying: Missing template tasks/delete_task.erb in view path app/views.
How can i make it not search for a template?
I tried with adding :method => :delete at the end of link_to_remote and changing my action name to destroy. I also added render :nothing => true. I also played with routes a bit. But still i allways get the same error.
The question is how can i make it not search for a template because i dont want it to render anything?
Would be very gratefull for any answers, Roq.
Have you got the same error when adding render :nothing => true? That's strange.
Rails is trying to search for a template because there's no render in your action. So to avoid it, you need to explicitly call the method:
def delete_task
task_pers = TaskPersonalization.find(params[:task_pers_id])
horse_task = task_pers.task
task_pers.destroy
if horse_task.task_personalizations.empty?
horse_task.destroy
end
render :nothing => true, :status => 200
end