I'm trying to implement a link, when clicked marks you as present for a meeting. This link is a method in a helper:
def link_to_remote_registration(event_id)
down_image = "down_blanco.png"
up_image = "up_blanco.png"
unless registration.nil?
if registration.present == 1
up_image = "up_filled.png"
elsif registration.present == 0
down_image = "down_filled.png"
end
end
link_to_remote_registration = String.new
loading_and_complete = "Element.show('indicator_event_"+event_id.to_s+"'); Element.hide('vote_"+event_id.to_s+"')".html_safe
complete = "Element.hide('indicator_event_"+event_id.to_s+"'); Element.show('vote_"+event_id.to_s+"')".html_safe
link_to_remote_registration =
link_to(image_tag(up_image , :id => 'will_not_attend_event_'+ event_id.to_s , border => 0),
:url => new_registration_path(:present => 1, :event_id => event_id, :escape => false),
:remote => true,
:method => :put,
:loading => loading_and_complete,
:complete => complete)
return link_to_remote_registration
end
The problem is that when I render the link in my view some of the html gets escaped making the link not work.
<a href="/calendar?complete=Element.hide%28%27indicator_event_1%27%29%3B+Element.show%28%27vote_1%27%29&loading=Element.show%28%27indicator_event_1%27%29%3B+Element.hide%28%27vote_1%27%29&method=put&remote=true&url=%2Fregistrations%2Fnew%3Fevent_id%3D1%26present%3D1">
<img id="will_not_attend_event_1" border="0" src="/images/up_blanco.png?1198181114" alt="Up_blanco">
</a>
Which I think is not a valid url. I wonder why this happens - i call the html escape on the complete and loading string.
Regards
Since you're passing the html from from a helper, Rails sanitizes it to protect from XSS. You can override it by returning:
link_to_remote_registration.html_safe
http://railscasts.com/episodes/204-xss-protection-in-rails-3
You could also use raw() instead of disabling XSS system-wide.
raw(image_tag(up_image , :id => 'will_not_attend_event_'+ event_id.to_s , border => 0))
Related
I want to pass a parameter using link_to. (Also I am trying to use Bootstrap tab)
ilban.html.erb
<%= link_to '일반공지', '#home', { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab', :where => 1 } %>
cpu_controller.rb
#where = params[:where]
This code doesn't get where as an parameter. How can I fix it?
I have not tested it, but it should pass the params that you want.
link_to "Search", searches_path(:where => 1, :when => "Today"), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
Controller:
#where = params[:where]
In Rails 5, try this syntax for link_to
link_to 'Show', view_path(:id => view.id), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
In the place of view path you can edit with your controller path and pass the valid id that you need to link.
Or, try this syntax also to pass params
<%= link_to "Add car", {:controller => "car", :action => "add_car", :car => car.id }%>
And add in your controller
#car = Car.find(params[:car])
newbe question here.
I've inherited a rails project and been asked to change some chunk of code, but I have a very little experience in Ruby and Rails (I am an objective-c with a PHP background developper)
So the first stuff I have to had is a textfield with an auto complete mode. Actually my code has this code for the auto completed text field :
registration_controller.rb
def auto_complete_for_training_title
puts "auto_complete_for_training_title"
current_locale = params[:locale].to_s
if current_locale == 'en' then
if current_user.member then
Locale.set current_user.member.written_language
else
Locale.set 'fr'
end
end
#trainings = Training.find(:all, :conditions => ["((title LIKE ? OR fso_id LIKE ?) and is_private = 0", '%' + params[:training][:title] + '%', '%' + params[:training][:title] + '%'])
puts #trainings.count
render :partial => 'complete_ajax_own_function_training'
end
In the view, the text field is created like this :
<% form_for :sortRegistration, { :url => { :action => 'index'} } do |f| %>
<p><label><%= "Display registrations by training".t %> :</label>
<%= text_field_with_auto_complete :training, :title, { :size => 50, :tabindex => 1 },
{ :select => 'mon_titre',
:after_update_element => "function(element,val)
{
var nodes = val.select(['.value_title']) || [];
if(nodes.length>0)
{
$('sortRegistration_training_id').value = Element.collectTextNodes(nodes[0], 'value_title');
}
}"
}
%></p>
<input id="sortRegistration_training_id" name="sortRegistration[training_id]" type="hidden" size="20" value="" />
<% end %>
If I copy / paste the code, only one textField works, I've tried to change some parameters but still not changing something.
P.S. As I said I am new in the RoR, I do not know if you need more code for help me so please, feel free to ask more, I will edit my question
View
<%= link_to "Link", {:action => "AjaxView",:col => "colname"},
:update => "Ajaxcall", :remote => true %>
Controller
def AjaxView
#vars= Var.find(:all,:conditions => { :varName=> "one" },:select=>(params[:col]))
respond_to do |format|
format.js { render :layout=>false }
end
end
AjaxView.js.erb
if ( (<%= col %>) == "colName") {
$("#3").text("<%= escape_javascript(render(:partial => "var") %>");
}
else
{
$("#2").text("<%= escape_javascript(render(:partial => "var") %>");
}
Issue here is this Variable "col" doesn't get its exact value in AjaxView.js.erb file, and my If else condition doesn't get executed. What am i missing here ?
The local variable col isn't assigned in AjaxView.js.erb. I think you meant to write it as params[:col].
Also the double quotes around var should be single quotes, to play nice with the surrounding double quotes.
{:action => "AjaxView",:col => "colname"}
This is not how you build a url for link_to.
What's the route to access the AjaxView method?
It should be something like: link_to "Link", AjaxView_resources_path(:col => "colname").
And in your controller, you need to set an instance variable #col = params[:col] to be able to use it in your view.
ps: your should name your method ajax_view instead of AjaxView.
if ( #col.to_s == "colName") {
$("#3").text("<%= escape_javascript(render(:partial => "var") %>");
}
elsif ( #col.to_s == "colName1")
{
$("#2").text("<%= escape_javascript(render(:partial => "var") %>");
}
I have a paginated list, and a select box of numbers showing how many pages to display. When the user picks a value I want the screen to refresh with the resized list.
View:
<%= select_tag :paginate_size, options_for_select([['Display: 5', 5],['Display: 10',10],['Display: 20', 20],['Display: 30', 30],['Display: 50', 50],['Display: 100',100]]), :onchange => remote_function(:url => {:action => :show_my_entries}, :with =>"'paginate_size='+value")%>
When I select a value I can see in the log that the show_my_entries function is being called and sent the paginate_size parameter.
In the controller I set the per_page variable as shown below, however selecting anything in the list doesn't refresh the screen with the new list size:
def show_my_entries
if (params[:paginate_size].nil?)
paginate_size = 8
else
paginate_size = params[:paginate_size]
end
#entries = Entry.find_all_my_entries_by_pub(session[:publication_id], session[:user_id])
#entries_paginate = Entry.paginate :page => params[:page], :order => 'title', :per_page => paginate_size, :conditions => "publication_id = " + session[:publication_id].to_s + " AND writer_id = " + session[:user_id].to_s
render :layout => "dashboard"
end
You're missing the :update parameter in the remote_function which should point to the id of the HTML element containing the entries list.
So, if the content is, for example, done like this:
<div id="my_entries">
<%= print_entries_list %>
</div>
Your remote function should be like this:
remote_function(:update => "my_entries", :url => {:action => :show_my_entries}, :with =>"'paginate_size='+value")
I want to create a very simple search partial. It has a text box, to query, and search db. Can I create a remote_function call without using AJAX or JS? Can I keep it entirely "Rails-ee"?
<%= text_field_tag "search_term",'', :size => 10 %>
<%= button "search", :onclick => remote_function( :url => {:action => :fill_in_lots },
:with => "search_term" ) %>
This isn't a problem, you need to use a technique called formal link. Instead of button you put a from with submit button. Below is a code of helper I use for this:
def formal_link_to(*args, &block)
options = html_options = name = nil
if block_given?
options = args.first
html_options = args.second
name = capture(&block)
else
name = args.first
options = args.second || {}
html_options = args.third
end
method = html_options.delete(:method) || "POST"
method = method.to_s.upcase
url = url_for(options)
html = "<form class=\"formal-link\" action=\"#{url}\" method=\"post\">"
html += "<input type=\"hidden\" value=\"#{form_authenticity_token}\" name=\"authenticity_token\" />"
html += "<input type=\"hidden\" value=\"#{method}\" name=\"_method\" />"
html += link_to(name, "#", html_options)
html += "</form>"
if block_given?
concat(html)
else
return html
end
end
You use this helper like a normal link_to, but you can pass extra options :method in second hash. Example:
<%= formal_link_to "Fill in lots", { :action => "fill_in_lots" }, { :method => :post } -%>
Remarks:
1. This of course will make the full page reload, but it is inevitable without using JavaScript.
2. I assumed action fill_in_lots is exposed to POST request. In case of GET you can use normal link_to helper.