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
Related
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.
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.
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
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.
I am currently trying to program my first ajax interface using Rails.
The application currently shows a table populated with list items. The user has to approve or reject each of the list items. I currently have an edit link at the end of each row that shows a form in which I can approve the list item.
I am thinking on using a checkbox instead of the edit link. When the user clicks the checkbox I want to update the database with the status, user name and date/time without leaving this page.
What steps should I follow?
Can I use a checkbox or am I
restricted to buttons?
What xxx_remote helper should I use?
How can I update the checkbox state with the results of the ajax call?
I don't think that a checkbox is the correct control for what you're looking for.
You said you want user's to be able to approve or reject items which means that you have 3 states: unhandled, approved and rejected. A checkbox only supports 2 states: off and on
I would use two links accept and reject and then do it as follows.
In your view:
...
<tr id="item1">
<td>Accept or Reject</td>
<td>
link_to_remote 'accept', :action => :accept, :id => 1, :method => :post
link_to_remote 'reject', :action => :reject, :id => 1, :method => :post
</td>
</tr>
...
In your controller
def accept
item = Item.find(params[:id])
item.accept
respond_to do |want|
want.js {
render :update do |page|
page << "$('item_#{item.id}').cells[0].innerHTML = 'Accepted'"
...include other updates you need to make to your row...
page.visual_effect :highlight, "item_#{item.id}"
end
}
end
end
... similar for reject method ...
This is a comment to solution proposed by Andrew,
I had to write params of link_to_remote function like this
link_to_remote 'reject', :url => {:action => :reject, :id => item.id, :method => :post}
Also, remember to add new actions to your routes.rb if You are using restful resources
i.e.
map.resources :items, :member => {:accept => :post, :reject => :post}