can't pass locals to parital Rails - ruby-on-rails-3

I have a partial _new_user_form.html.erb
<%= form_for(#user, :remote => true, :html => {:id => 'new_user_form'}) do |f|%>
<strong><%= :form_text %></strong>
<%= f.text_field :email, :placeholder => get_placeholder_text(#board), :size => "30" %>
<%= hidden_field_tag :role, role %>
<%=f.submit "SAVE", :class => "button-small" %>
<% end %>
In the show.rb I want to use it and pass in some partial variables as follows:
<%= render 'users/new_user_form', :locals=> {:role => "Celebrant" } %>
However I get this error:
undefined local variable or method `role' for #<#<Class:0x00000103d5e8b0>:0x00000103d5b930>
I read the documents about passing in locals and this seems correct. What am I doing wrong?

You're combining the short and long forms. Either of these are correct (identical):
render 'my_partial', :foo => 'bar'
render :partial => 'my_partial', :locals => { :foo => 'bar' }

I think you're calling render incorrectly. From the fine manual:
If no options hash is passed or :update specified, the default is to render a partial and use the second parameter as the locals hash.
So you end up going down this branch in the source:
view_renderer.render_partial(self, :partial => options, :locals => locals)
and that makes your call the same as this:
render :partial => 'users/new_user_form', :locals => { :locals => { :role => 'Celebrant } }
Note the extra level of nesting for :locals. Try this:
render 'users/new_user_form', { :role => 'Celebrant' }
I'm looking at (and using) 3.1 so your version might be a little different.

Related

Partial not accessing local variable

I am rendering a partial like so:
<% #pages.each do |page| %>
<%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
<% end %>
But when i try to access a variable in page i am getting the error:
undefined local variable or method `page'
I am accessing the variable like:
<%= page.title %>
So what else do I need to do?
i'm not 100% sure but isn't it either
<%= render 'layouts/pagewithchildren', :page => page %>
or
<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>
?
You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.
Change your code to:
<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>

Default value on select field in formtastic form with no model

I have a formtastic form to gather parameters for report generation. I know formtastic is designed to be used with models but I need to use it here as the form is in an activeadmin page.
It's all working well but I can't set a default value for the selects. I'm willing to implement a "despicable hack" to get this working. I'd prefer not to implement a fake model
just to set default values on a form.
The form code looks like this
<%= semantic_form_for "report", :url => report_admin_payments_path do |f| %>
<%= f.inputs do %>
<%= f.input :report_type, :as => :select, :collection => report_types, :input_html => {:style => "width:180px"} %>
<%= f.input :start_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => 1.month.ago.strftime("%Y-%m-%d")} %>
<%= f.input :end_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => Time.zone.now.strftime("%Y-%m-%d")} %>
<%= f.input :country, :as => :select, :collection => locales, :input_html => {:style => "width:180px"}%>
<% end %>
<%= f.actions :submit %>
<% end %>
Thanks in advance,
Matt
This or something similar should meet your needs.
class LightModel
# Subclass this for a model that looks like an ar model but has no persistence
# good for formtastic forms
include ActiveModel::Naming
include ActiveModel::Validations
def initialize(attributes = {})
#attributes = attributes
end
# read only atm
def method_missing(m, *args, &block)
#attributes[m]
end
end
Thanks,
Matt

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"

Rails 2 to rails 3 link_to image_to tag

This is the code that I use in rails 2
<%= link_to_remote image_tag("icon_edit.png", :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()" ).html_safe, :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :html => { :class => "link_grey" },:before => "$('edit_contact_link').hide();show_spinner('view_contact_detail','view_contact_detail_spinner')" %>
How can I make it to rails 3 ?
I am new to unobtrusive javascript so someone can help me in converting this ?
I tried using the following :
<%= link_to :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :remote=>true do %>
<%= image_tag('icon_edit.png', :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()") %>
<% end %>
But I am missing the :before option :(
Thank you
Instead of using :before as you would do in Rails 2.X, define it as a :onclick.
<%= link_to :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :remote => true, :onclick => "$('edit_contact_link').hide();show_spinner('view_contact_detail','view_contact_detail_spinner')" do %>
<%= image_tag('icon_edit.png', :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()") %>
<% end %>
The onclick event should be run before the :remote call.
See my comment on your question as to why the remote part isn't running.

Form_tag remote in Rails 3 in partial

To become member, A user can use a button to create a membership on the community page. This is made on a partial that create a new "membership"
the memberships_controller :
#community = Community.find(params[:community_id])
#community.memberships.create(:user => current_user, :role => 1)
in The view :
<% form_remote_tag :url => community_memberships_path(#community) do %>
<%= submit_tag 'Join' %>
<% end %>
After upgrading to Rails 3, that doesn't work anymore !
I tried this :
<% form_tag( {:url => community_memberships_path(#community)}, :remote => true) do %>
and this :
<% form_tag :url => {:controller => '/memberships/new', :action => :create,:community_id => #community }, :remote => true do %>
But no chance.. and have this error
No route matches "/communities/2
Thx for help
The URL is the first argument, options are secondary.
<% form_tag(community_memberships_path(#community), :remote => true) do %>
...
As for your No route matches exception, check you have translated you routes to the Rails 3 syntax correctly.