Specifying :class of embedded ruby form - ruby-on-rails-3

If I would like to assign a class to my embedded ruby form, like so?:
<%= form_for(User.new) do |f|, :class => "form-horizontal" %>
How could I go about doing it? I keep getting a syntax error.
Thanks!

from http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
form_for(record, options = {}, &proc)
meaning:
<%= form_for(User.new, { :class => 'form-horizontal' }) do |f| %>

Related

undefined method `model_name' in a partial rendered by different controller

I'm trying to render this form:
<form class="form-inline">
<%= simple_form_for #prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Name', label: false %>
<%= f.input :email, placeholder: 'Email', label: false %>
<%= f.input :interests, placeholder: 'Tell us what you were searching for', label: false, value: params[:search] %>
<%= f.error :base %>
<%= f.button :submit, "Submit", :class=> "btn" %>
<% end %>
Using this partial:
<%= render partial: 'prospects/novideo_capture' %>
The partial is in a view controlled by Videos#index controller, and I keep getting this error: 'undefined method `model_name' for NilClass:Class'
This is my prospects controller:
class ProspectsController < ApplicationController
def index
#prospects = Prospect.all
end
def new
#prospect = Prospect.new
end
def create
#prospect = Prospect.new(params[:prospect])
if #prospect.save
render "thanks_for_interest"
else
render "novideo_capture"
end
end
I'm not sure what I'm going wrong, although I'm pretty sure it's a simple solution. I've seen a lot of similar questions around SO and tried all their answers, but none of them seem to work for this situation.
Thanks for any help...
EDIT: Adding
#prospect = Prospect.new
to the videos index controller stops the error occurring, but I don't feel it's the right way to do this. It also doesn't actually make the form use the prospects controller.
EDIT2: I now have the partial rendering correctly (I think), and my videos#index calls the partial like this:
<%= render partial: 'prospects/novideo_capture', :prospect => #prospect %>
Then simple_form in the partial looks like this:
<form class="form-inline">
<%= simple_form_for :prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
...
<% end %>
However it's not actually submitting the form with the prospects controller. Any ideas why?
Check your markup. You're wrapping a simple_form inside another form. Since the first form tag has no action associated with it (<form class="form-inline">), that form will submit against the current URL, which is the video#index.
You're going to want something like this:
<%= simple_form_for :prospect, :url => etc, :method => 'post', :class => "form-inline" do |f|
...
<% end %>
Losing the leading (redundant) form-inline form tag and you'll be fine.

Rails 3 + Simple Form: Specifiy label class when using f.association

How can I specify the label class when using f.association instead of f.input in simple_form?
For example, this works:
f.input :name, :label_html => { :class => 'some-class' }
But this doesn't
f.association :periods, :as => :check_boxes, :label_html => { :class => 'some-class' }
Meaning that the label related to :name will have some-class as part of its class, but the label related to :periods won't. Any way to do this without changing f.association to f.input? Thank!
I think you can't add custom class to each label but you can do it for each item wrapper, e.g:
<%= simple_form_for(#user) do |f| %>
<%= f.association :group, as: :check_boxes, item_wrapper_class: 'custom-class' %>
<%= f.button :submit %>
<% end %>

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.