How to pass variables to partial in Rhomobile - rhomobile

I have a partial where i need show the value of variable catchedEvent,
Partial
<div class="eventList">Here to show the value of the variable</div>
On my erb page i'm calling this using
<%= render :partial =>"mypartial" %>
I can't get the way to pass the variable to it. Please help.

You can use the locals or collections as specified over here http://docs.rhomobile.com/rhodes/ui#advanced-usage-of-render
<%= render :partial =>"mypartial", :locals => { :event => "myevent" } %>
Then on partial, use it like
<div class="eventList"><%= event %></div>
Second, if you assign some value to the instance variable in the controller,
#event = "myevent"
then you can directly access it inside the partial as below,
<div class="eventList"><%= #event %></div>
These are some of the ways you can use to pass the variables.

Related

How to use local variables when calling show on an object i.e. #object.this_#{variable}? [duplicate]

This question already has answers here:
How to turn a string into a method call? [duplicate]
(3 answers)
Closed 5 years ago.
Is there any way to pass a variable through to an instance in a show partial? Basically I'm creating a service that follows CRUD and want to be able to iterate an instanced variable. I can create it by passing a partial like so:
In the parent view:
<%= form_for(#report) do |f| %>
<%= render :partial => "reports/forms/partial_form", :locals => {:g => f, :var => "01"} %>
<% end %>
In the partial view:
<label><%= "Enter Data for Variable #{lane_number}" %></label>
<%= g.text_field :"Variable_#{var}" %>
This is useful the variables in the partial have a similar-enough naming convention to use the same var to iterate through several items. This works just fine.
I would like to do something similar when viewing the input data, but I can't figure out how to pass that var to the instance variable.
In the parent view:
<%= render :partial => "reports/shows/partial_show", :locals => {:var => "01"} %>
In the partial view:
<%= #report.Variable_#{var} %>
I know that #{var} is just used for string interpolation (which is why it works in the original create), but is there a way for me to do this by passing the variable to the partial?
You can use metaprogramming to get variable value because you just call a method:
<%= #report.send("variable_#{var}") %>

How do I pass a variable into this partial to use as a counter?

<div class="postSlideshowDots">
<%for i in #post.images%>
<%=render :partial => "dots", :locals => {:image => i }%>
<%end%>
</div>
I would like to also pass in a variable (=0 when it goes in) to use as a counter. I had read that there was one already built in, "partialname_counter", but that gave me an error.
use <%= render :partial => "dots", :collection => #post.images %>, and you will get a variable called dots_counter in the partial. You will also need to change your partial to use dots instead of image as the variable.
Finally, don't use for..in in Ruby, it is non-idiomatic, and has some bad behavior in regards to closures and scoping. Use .each

Ruby on Rails pass reference to yield (template)

To make long story short: each of my tabs has its own form, so I decided to make a single layout and just to have a forms themselves as a variable content for a layout.
But I need to have form_for to be in a layout, rather then having it in each of the forms, because I have some other common form elements in the layout.
So how can I pass the form builder's reference f to the template ?
Layout code:
<% content_for(:content) do %>
<%= form_for current_form do |f| %>
<%= yield %>
<%= f.submit "Submit" %>
<% end %>
<% end %>
Is it possible ?
P.S Found this one: DRYing up a helper: wrap form_for and access local form variable (#rubish's answer), but <%= yield f %> doesn't seem to be working, f still remains undefined for the view.
Why don't you make a common template (not layout) for the tabs, and use a partial template for the content of each tab?
Then you can do something like:
<%= render :partial => #tab_name, :locals => {:form => f} %>
You can render a template in Rails that accepts a block by using the layout option of render
Let's say you have a form that you render a few times but you would like to customize your submit section each time. You can achieve this by rendering your form partial as layout and passing in a block. Your template or partial then serves as the surrounding layout of the block that you pass in. You can then yield back the form to the block and access the form in your block.
record/_form.haml
= form_for record do |form|
...
.form-actions
yield(form)
In order to make your template record/_form.haml accept a block when you render it, you can render your template as the layout for your block using the layout option of render:
record/edit.haml
= render layout: 'form', locals: { record: record, ... } do |form|
.form-actions--primary
= form.button :submit

Pass Parameters in render - Rails 3

I've seen a couple questions on this but haven't been able to solve it...
I'm trying to pass a parameter while rendering a partial (similar to domainname.com/memory_books/new?fbookupload=yes)
Right now, I use this line:
<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>
and in the partial, I have tried to get the content of fbookupload by using:
<%= fbookupload %>
which gives an "undefined local variable" error and
<%= params.inspect %>
which does not show fbookupload as a parameter.
How can I have the partial pass along the parameter :fbookupload?
Thank you.
UPDATE:
Could it have anything to do with the fact that I'm rendering this within a render?
i.e. the page (/fbookphotos/show) that has
<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>
is being rendered by another page with (posts/show) via:
<%= render :partial => '/fbookphotos/show' %>
so I'm rendering this within a render.
try this:
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => "yes"} %>
Taking it out of the comments for posterity. This syntax is correct:
render '/memory_books/new', fbookupload: "yes"
But if there is a reference to rendering the same partial without specifying the local variables, e.g.
render '/memory_books/new'
then fbookupload variable becomes unavailable. The same applies to multiple local variables, e.g.
render 'my_partial', var1: 'qq', var2: 'qqq'
will work if only occurs once. But if there is something like that somewhere else in the code
render 'my_partial', var1: 'qq'
then the var2 will become unavailable. Go figure ...
To do it your way:
In the main view:
<% fbookupload = "yes" %>
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => fbookupload} %>
And in the partial:
<%= fbookupload %>
2nd option:
Ideally in the controller, otherwise in the view, define an instance variable: #fbookupload = "yes". Then it is available everywhere. The partial will then be : <%= #fbookupload %>
Params is just request parameter, so if u want to pass it in params u have to add it to your url ?fbookupload=yes or assign it params[:fbookupload] = "yes", but i don't think that is a good idea.
But if u need to use params[:fbookupload]', u can replace it withparams[:fbookupload] || fbookupload', and pass fbookupload in locals hash for partial.
render can be called with or without the partial param, and there seems to be some confusion around the differences between these two forms.
The following two are equivalent:
<%= render "my_partial', my_param: true %>
and:
<%= render partial: "my_partial', locals: { my_param: true } %>
The first is a shorthand that allows you to omit partial:. With this shorthand, local variables are also not nested under locals:. This is explained well in the documentation (see 'Rendering the default case').
In the two cases above, you would access my_param in the partial directly with my_param.
One other source of confusion is that if you render the partial somewhere without passing my_param, then the partial will fail when it tries to access it. To get around this, you can access the local with local_assigns[:my_param] instead of my_param, which will give you nil if the param is not defined instead of erroring, as described in this documentation. Another alternative is to use defined?(my_param) before accessing it.

Rails 3, proper way to do partials?

Have the following code throwing an error. I've always just jury-rigged something before but want to learn the proper way. Is the proper way to pass the local variables? Say you also have an instance variable you want to use in the partial?
#users is an array of user emails
<% #users.each do |email| %>
<%= render :partial => 'user/foobar' %>
<% end %>
user/foobar
<div id="user_<%= email %>>
<div><%= email %></div>
</div>
EDIT: also will want to use the partial in a js file after adding an email address. Would I set the instance after create to #user or something else?
You don't need to use each. You can use the :collection option:
<%= render :partial => "user/foobar", :collection => #users %>
In user/_foobar.html.erb partial (below, the automatically-created local variable foobar will contain the current instance of an item from the #users collection):
<div id="user_<%= foobar %>>
<div><%= user %></div>
</div>
The local variable is named after the name of the partial. See guide here.
try this:
render :partial => 'user/foobar', :locals => {:email => email }
this passes-in your local variable 'email' as the symbol :email

Resources