form_tag doesn't work - forms

I am a bit new to rails and I have the following code:
<% form_tag "/producers/#{#user.id}", method: :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
It doesn't do anything. Replaced it with
<form action="/producers/<%= #user.id %>" method="get">
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
</form>
and all is fine. I know I should do producer_path(#user) but I am trying to find out what's wrong.
What am I missing?

You have the line
<% form_tag %>
this won't print anything to the page
<%= %>
is the ERB tag for when you want to print things
<%= 'hello' %>
prints hello
<% 'hello' %>
merely evaluates it

Related

ArgumentError for partial form, First argument in form cannot contain nil or be empty

My registration form was working just fine, but once I moved the registration from into a partial _signup.html.erb it has give me the error "First argument in form cannot contain nil or be empty"
I want to have the user registration from go through a modal from from the homepage instead of being redirected to new.html.erb.
static_pages/home.html.erb
<% else %>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Sign up with email
</button>
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Sign up</h4>
</div>
<div class="modal-body">
<%= render partial: 'users/signup', locals: {user: #user} %>
</div>
</div>
</div>
</div>
<% end %>
and in the partial _signup.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<form id="SignupForm">
<%= render 'shared/error_messages' %>
<fieldset>
<legend>Fill in your info</legend>
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<fieldset>
</fieldset>
<legend>Languages</legend>
<label for="Gender">Select gender</label>
<%= f.select :sex, options_for_select([['Select Gender', ''],['Male','1'],['Female','2']], "#{#user.sex}") %>
<label for="Country">Fill in your location</label>
<%= f.country_select :country_code, { priority_countries: ["GB", "US"] }, class: 'required-field', required: true %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</fieldset>
</form>
</div>
</div>
part of the users_controller.rb
class UsersController < ApplicationController
respond_to :html, :js
before_action :logged_in_user, only: [:index, :show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
#users = User.paginate(page: params[:page])
end
def show
#user = User.find(params[:id])
#page_name = "user_page"
end
def new
#user = User.new
end
def signup
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
routes.rb
Rails.application.routes.draw do
root to: 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get "users/signup" => 'users#signup'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
You don't have access to the instance variable #user in a partial. You have to pass it as a local variable:
static_pages/home.html.erb
<div class="modal-body">
<%= render partial: 'users/signup', locals: {user: #user} %>
</div>
And in your form partial you have to use that user local variable:
_signup.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(user) do |f| %>
<form id="SignupForm">
<%= render 'shared/error_messages' %>
<fieldset>
<legend>Fill in your info</legend>
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<fieldset>
</fieldset>
<legend>Languages</legend>
<label for="Gender">Select gender</label>
<%= f.select :sex, options_for_select([['Select Gender', ''],['Male','1'],['Female','2']], "#{user.sex}") %>
<label for="Country">Fill in your location</label>
<%= f.country_select :country_code, { priority_countries: ["GB", "US"] }, class: 'required-field', required: true %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</fieldset>
</form>
</div>
</div>

Rails form validation: how to properly display the error for a checkbox

From what I can tell, my question should be pretty easy to answer for someone who encountered this issue before ...
I have a signup form in rails which should highlight the fields that are not properly completed when the submit button is clicked (it should also show relevant error messages). The form contains only input fields and a checkbox. While the errors are properly displayed for each part of the form, the checkbox label is not properly highlighted in red when the user tries to submit the form whithout checking the checkbox.
What should I modify in my code to have the checkbox label highlighted when the checkbox is left unchecked?
This is the view code:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<%= f.check_box :terms_of_service %>
<label_for="user_terms_of_service">I agree to the <%= link_to "Terms of Service", policies_path %>
</label>
<br><br>
<%= f.submit "Create my account", :class => "btn btn-large btn-primary" %>
<% end %>
Also, this is what I can see with Firebug for the checkbox after the form is improperly submitted - the problem is that the div with class="field_with_errors" is put before the checkbox label.
<br>
<input type="hidden" value="0" name="user[terms_of_service]">
<div class="field_with_errors">
<input id="user_terms_of_service" type="checkbox" value="1" name="user[terms_of_service]">
</div>
<label_for="user_terms_of_service">
Thank you for your help,
Alexandra
My approach with forms is to try and use the rails standards where possible, e.g.
Add to your User model:
validates_acceptance_of :terms
attr_accessible :terms
In your view,
<%= form_for #user do |f| %>
...
<%= f.check_box :terms %>
...
<% end %>
You actually don't need an extra column in the users table unless you want to grant access to users who have not accepted the terms of service, which... don't exist as they can't complete registrations in the first place.
The label syntax is not correct. Try
<%= f.label :user_terms_of_service, "I agree to the #{link_to 'Terms of Service', policies_path}.".html_safe %>

Access subcollection in mongoid with rails form to edit and create new entry

I am using mongodb with mongoid in rails3. I am newbie to all this. My models are as shown below.
class Californium
include Mongoid::Document
field :first_name
field :License_type
Field :Licese_expiry_date
embeds_one :address
end
class Address
include Mongoid::Document
field :street
field :city
field :state
field :zip
embedded_in :Californium, :inverse_of => :address
end
My Controller
class CaliforniaController < ApplicationController
def index
#california = Californium.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #california }
end
end
def show
#californium = Californium.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #californium }
end
end
# Here is where I have problem. I am not able to show a
# form with californium address details. My form view is show after the controller
def new
#californium = Californium.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #californium }
end
end
def edit
#californium = Californium.find(params[:id])
end
end
My form
<%= form_for(#californium) do |f| %>
<div class="field">
<%= f.label :license_number %><br />
<%= f.text_field :license_number %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :license_type %><br />
<%= f.text_field :license_type %>
</div>
<div class="field">
<%= f.label :license_status %><br />
<%= f.text_field :license_status %>
</div>
<div class="field">
<%= f.label :license_expire_date %><br />
<%= f.text_field :license_expire_date %>
</div>
<div class="field">
<%= f.label :license_issue_date %><br />
<%= f.text_field :license_issue_date %>
</div>
<div class="field">
# Here I am not able to access :address.street and :address.city as it is an other
# model embedded in californium
<%= f.label :address.street %><br />
<%= f.text_field :address.street %>
</div>
<div class="field">
<%= f.label :address.city %><br />
<%= f.text_field :address.city %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I am trying to build a form where in all the details of the californium could be edited. I am not able to access californium's address details as it is a subcollection of californium collection. I am able to display all the details of the californium including the address but dont how to create a editable form.
Try using the form helper "fields_for" for your embedded model as in the following working example.
Hope that this helps.
It took a while to wade through the typos and inconsistencies, so if you want a faster answer in the future,
please make your question both as accurate and as minimal as possible.
class Californium
include Mongoid::Document
field :name
field :license_type
embeds_one :address
end
class Address
include Mongoid::Document
field :street
field :city
field :state
field :zip
embedded_in :california, :inverse_of => :address
end
app/views/edit.html.erb
<%= form_for :californium do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :license_type %>
<br/>
<%= f.text_field :license_type %>
</div>
<%= fields_for #californium.address do |af| %>
<div class="field">
<%= af.label :street %>
<br/>
<%= af.text_field :street %>
</div>
<div class="field">
<%= af.label :city %>
<br/>
<%= af.text_field :city %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
config/routes.rb
match 'california/:id' => 'california#edit', via: :get
match 'california/:id' => 'california#update', via: :post
test/functional/california_controller_test.rb
require 'test_helper'
class CaliforniaControllerTest < ActionController::TestCase
def setup
Californium.delete_all
end
test "form" do
cal = Californium.create(name: 'Benjamin Spock', license_type: 'MD', address: Address.new(street: '311 Temple St', city: 'New Haven', state: 'CT', zip: '06511'))
assert_equal(1, Californium.count)
p Californium.find(cal.id)
get :edit, id: cal.id
assert(assigns(:californium))
assert_response(:success)
puts #response.body
end
end

Can't validate phone number

I'm trying to validate a phone number without success. When I submit the form for the following model, it always accepts the phone number I put in, whether it's valid or not. Why is this happening?
class Client < ActiveRecord::Base
belongs_to :salon
belongs_to :address
accepts_nested_attributes_for :address
attr_accessible :address_attributes, :name, :phone, :email
validates_presence_of :name
validates_presence_of :email
validates_presence_of :phone,
:unless => Proc.new { |c| c.phone.gsub(/[^0-9]/, "").length != 10 }
end
-
<%= form_for(#client) do |f| %>
<% if #client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#client.errors.count, "error") %> prohibited this client from being saved:</h2>
<ul>
<% #client.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :salon_id, :value => Salon.logged_in_salon.id %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<%= f.fields_for :address do |address_form| %>
<div class="field">
<%= address_form.label :line1 %><br />
<%= address_form.text_field :line1 %>
</div>
<div class="field">
<%= address_form.label :line2 %><br />
<%= address_form.text_field :line2 %>
</div>
<div class="field">
<%= address_form.label :city %><br />
<%= address_form.text_field :city %>
</div>
<div class="field">
<%= address_form.label :state_id %><br />
<%= select("client[address]", "state_id", State.all.collect {|s| [ s.name, s.id ] }) %>
</div>
<div class="field">
<%= address_form.label :zip %><br />
<%= address_form.text_field :zip %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should be using validates_format_of instead of validates_presence_of. Something like:
validates_format_of :phone,
:with => /\A[0-9]{10}\Z/,
:allow_blank => true,
:allow_nil => true

How to set a file_upload control for a field in rails_admin?

I'm new to rails and I recently discovered rails_admin.
How to set a file_upload control for a field in rails_admin?
If you are using the Carrierwave gem for your file uploads you can do something like this
https://gist.github.com/884835 or in erb if you prefer that:
<%= label_tag "#{field.abstract_model.to_param}_#{field.name}", field.label %>
<div class="input">
<% image = field.bindings[:object].send(field.name) %>
<% if image.path %>
<div class="row">
<% default_version = image.versions[:main] %>
<%= image_tag default_version && default_version.url || image.url %>
<br />
<%= form.check_box "remove_#{field.name}" %>
<%= form.label "remove_#{field.name}", "Remove existing #{field.label.downcase}", :class => "inline" %>
</div>
<% end %>
<div class="row">
<%= form.file_field field.name, :class => "fileUploadField #{field.has_errors? ? "errorField" : nil}" %>
<%= form.hidden_field "#{field.name}_cache" %>
</div>
</div>