Rails3 - How to send Javascript Variable to a controller's action with the link_to helper? - ruby-on-rails-3

If i have the following javascript code
var myVar = 0;
function setNewValforVar(newValue){
myVar = newValue;
}
and i'm calling setNewValforVar function n times
so when I click on a link it'd send myVar value to the controller action in a remote link like
<%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%>
I Rails 2.3.x I'd do this with
<%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%>
and i was getting this on the controller's action with
params["myVar"]
how do I do this on Rails 3 with the link_to helper?

Rails 3 no longer supports this behavior. You will have to either create a form to submit the value, or modify the href of the link to include the value in the query string.

I found some solution that use callbacks. Link must be marked in some way, for example by adding the id:
<%=link_to "My Link", {:action=>"myAction"}, :remote=>true, :id => "mylink" %>
There is an example for prototype_ujs: the parameter is simply appended to the request's URL (the code is a bit simplified I assume that some parameters already exist).
<%= javascript_tag("document.on('ajax:create', 'a#mylink',
function(event) { event.memo.request.url += '&sval=' + myVar; })") %>
Some advantage of this (ugly a bit) solution is the possibility of using one function for a particular class of links instead of the indicated id.

Related

Html.Action Puts nothing to the page ( WebView Syntax )

When I use Html.Action in webview syntax, it puts nothing to the page. I see it goes to the Navigation controller of the TopMenu action when I debug without any problem, but nothing appears to the page. Am I missing something ?
//I USE THIS ON MASTERPAGE
<% Html.Action("TopMenu", "Navigation"); %>
//IN CONTROLLER
public class NavigationController : BaseController
{
public ActionResult TopMenu()
{
return PartialView("TopMenu");
}
}
do like this:
<%= Html.Action("TopMenu", "Navigation") %>
instead of:
<% Html.Action("TopMenu", "Navigation") %>
You're using razor incorrectly. Instead of putting your code on a code block, you should use the # syntax in your view like so:
<%= Html.Action("TopMenu", "Navigation") %>
Or if you still want to use a codeblock, you can do Html.RenderAction helper instead. And that would write directly to the buffer(it's a little bit more efficient as well)

Use translation for submit button

I don't want to use the default
<%= f.submit %>
and have created a helper function for it, which also embeds an icon. The helper function expects a label to put on the newly created button.
I'm calling it like this:
<%= submit_button("icon-plus", I18n.translate("helpers.submit.create")) %>
But now on this text appears on the button:
%{model} toevoegen
Instead of:
Product type toevoegen
If I use the normal submit button then the correct text appears so my yml files are correct. How can I get the correct text to use in the helper?
Helper code:
def submit_button(icon, label)
link_to "javascript:void(0)", :class => 'btn btn-primary', :onclick => "$(this).closest('form').submit()" do
raw('<div class="') + icon + raw(' icon-white"> ') + label +raw('</div>')
end
end
As the I18n guide says, the translate function interpolates variables passed in the %{} brackets using its second argument (a hash).
In your case you need to tell it the model by doing this:
I18n.t("helpers.submit.create", model: "Product type")
If you want a generic option that would work for any model, you can see how Rails itself does it by looking at the source on GitHub - it's something like
I18n.t("helpers.submit.create", model: f.object.class.model_name.human)
As an aside, you don't need to (and probably shouldn't) use raw there. What you are trying to achieve could easily be done with the built-in helpers:
link_to ... do
content_tag :div, label, class: "#{icon} icon-white"
end

Rails and named_scope queries

I'm trying to get my head around the concept of named_scoped queries in rails.
I'm trying to filter a table to get only non featured items (:featured => false).
In my model i have added
scope :allgames, where(:featured => false)
and
scope :featured, where(featured => true)
I'm trying to list all featured and non featured items separately on my Game index page.
Is it possible to to it via a named scope.
So far i have:
<% #games.each do |item| %>
<% if item.featured %>
<%= render 'application/item_synopsis_builder', item: item %>
<% end -%>
<% end %>
And I wonder if it is possible to do something like:
<% #games.featured.each do |item| %>
<%= render 'application/item_synopsis_builder', item: item %>
<% end %>
or
<%= render partial: 'application/item_synopsis_builder', collection: #games.featured %>
When I try I get a message saying that there is no method featured.
But when I run the command Game.featured in the console I get the result list of all featured games.
Is it possible to access this list/method in the view?
Named scopes are added to the model as a class method, so trying to access the method on a collection of objects won't work. Similar functionality can be achieved with:
#games.where(:featured => true).each do
...
end
But I would recommend having two variables in your controller:
#featured_games = Games.featured
#all_games = Games.allgames
then use those in your views.
Your views are driven by the #games instance variable that is created by the controller that is rendering the views. Named scopes create a class method for subclasses of ActiveRecord::Base. So "Game.featured" returns something because defining the named scope created a method for the Game class. It did not create an instance method that objects of the Game class (such as #games) can invoke. That's why "#games.featured" gives you an error.
To do what you want to do, create two instance variable in the controller and pass them to the view, e.g.
#all_games = Game.allgames
#featured_games = Game.featured
Both variables will be available to your view, and you can construct loops to render each collection however you like.
A scope is a class method (or assimilable to, I don't know the specifics), so yes, Game.featured would work, but when you do #games.featured, you are calling featured on an array of Game instances.

saving file after passing parameter

Here is the parent question: save string to file
I want to pass the parameter which will be saved in file(.csv) after clicking button.
#bigtable is a table with strings in each row.
Here is the code in my show.html.erb:
...some code here...
<%= form_tag do %>
<% text_field_tag, id = "bigtable", value = #bigtable.to_s %>
<%= submit_tag 'Zapisz' %>
<% end %>
and my controller method:
def savefile
#bigtable = param[:bigtable]
#bigtable.join("\n")
File.open("path/to/file", "w") { |file| file.write #bigtable.join("\n") }
end
But mine code doesn't work :/
I want to save #bigtable strings to file. Each row record of the table is a new line of the file. And I want to save file without redirecting current page anywhere but completely don't know why:( Please help.
okay, I know why it doesn't work - I shoud add some new route to initialize savefile method - but how do it without redirecting/refreshing current page with results? plz help
Use <%= form_tag(url, :remote => true) do %> to make the call with Ajax, so your page will not be redirected. Use your server logs to see if the request is executed (if you want to get the result of the ajax call in your page, look at http://www.alfajango.com/blog/rails-3-remote-links-and-forms/).
I've found a solution - to not write double post here is the link to the topic with the answer: saving variable to file and downloading it

Reload calendar into nested forms (rails 3)

I have a form which contains one model (let's call it Model1) and this models accepts nested attributes from another one (let's call it Model2). I want to be able to save many Model2 records, but I want the view, at the beginning, to show just one set of fields from Model2, and have a button which, if clicked, displays another set of fields from that model, and so on. For this, i'm using the nested_form gem.
All is working well, but the problem is that one of the fields is associated with a datepicker. So, the first Model2 set of fields shows the calendar, but when I click to render the next set of fields, the datepicker, of course, does not get shown anymore (because the Javascript loads only when you get to the page and never again)
The following code shows the button which adds more concepts (Concept is Model2)
<%= f.fields_for :concepts do |concept_form| %>
<%= render "courses/concept_fields", :f=>concept_form%>
<% end %>
<p><%= f.link_to_add t("concepts.add"), :concepts %></p>
And inside courses/concept_fields I have:
<%= f.text_field :collection_date, :value => f.object.collection_date, :class => 'text date_picker tabs' %>
The javascript for the datepicker is included in the layout
Is there any way you can help me? Thanks!
Add and click event handler to the button you use to render the next set of fields to reload the datepicker something like:
$('.render_next_button').click(function(){
// code to reload datepicker
$( "#datepicker" ).datepicker();
});
I've found an answer which solved my problem. It's in this link:
jQuery UI DatePicker with nested_forms Gem
Would look like this:
<script type="text/javascript">
jQuery(function() {
jQuery('form').live('nested:fieldAdded', function(event) {
jQuery(event.field).find('.date_picker').removeClass('hasDatepicker').datepicker();
});
});
</script>