The following code is what im having syntax errors on. I tried following an example website, but I guess I must've put something in the wrong place or didnt add something. If someone could show me what i might've done wrong that would be great.
<div align ="center">
<h3>Create Your Project</h3>
<%= simple_form_for #createproject, :url => createprojects_url :html => {:class => 'form-horizontal'} do |f| %>
<%= f.input :name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :tripdates, :required => true %>
<%= f.input :teamname, :required => true %>
<%= f.input :teamleader, :required => true %>
<%= f.input :tree, as: :check_boxes %>
<%= f.input :solar, as: :check_boxes %>
<%= f.input :water, as: :check_boxes %>
<%= f.input :goats, as: :check_boxes %>
<%= f.input :kids, as: :check_boxes %>
<%= f.input :house, as: :check_boxes %>
<%= f.input :roof, as: :check_boxes %>
<%= f.input :latrine, as: :check_boxes %>
<%= f.input :build, as: :check_boxes %>
<% f.button :submit %>
<% end %>
</div>
simple_form_for #createproject, :url => createprojects_url, :html
=> {:class => 'form-horizontal'} do |f|
you've missed a comma
I'm using 'rails-settings-cached' gem and I want to add ability of creating/updating settings in backend. Model has fields: 'var', 'value', etc... As form builder I'm using Formtastic-bootstrap.
When I doing f.input :value I get no implicit conversion of nil into String. I think that the reason is that 'value' is a keyword.
How can I solve my problem?
Thank you!
UPD:
<%= semantic_form_for [:admin, #setting], :html => {:class => "form-horizontal"} do |f| %>
<%= f.semantic_errors :var, :value%>
<%= f.inputs do %>
<%= f.input :var %>
<%= f.input :value, :as => :text %>
<% end %>
<%= f.actions :class => 'form-group' do %>
<div class="col-lg-offset-2 col-lg-8">
<%= f.action :submit %>
<%= f.action :cancel %>
</div>
<% end %>
I have this model User, Entidade and Candidato.
class User < ActiveRecord::Base
has_one :entidade
has_one :candidato
accepts_nested_attributes_for :entidade
accepts_nested_attributes_for :candidato
class Candidato < ActiveRecord::Base
belongs_to :user
class Entidade < ActiveRecord::Base
belongs_to :user
Basically in order to register you need to specify if you want to be an Entidade or a Candidato. They have some shared attributes that i put in the User table. And the non shared attributes stay in the respective table.
This is the form:
<%= simple_form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.input :email %>
<%= f.input :role, :as => :hidden, :input_html => { :value => "candidato" } %>
<%= f.input :password %>
<%= f.input :password_confirmation, label: "Confirme a password" %>
<%= f.input :nome %>
<%= f.input :foto, :label => "Foto" %>
<%= f.input :cod_postal, :label => "Código-Postal" %>
<%= f.input :localidade %>
<%= f.input :contacto1, :label => "Contactos" %>
<%= f.input :contacto2, label: false %>
<%= f.input :pagina, :label => "Página Pessoal" %>
<%= f.fields_for :candidato do |ff| %>
<%= ff.input :data_nascimento, :label => "Data de Nascimento" %>
<%= ff.input :bi, :label => "Bilhete de Identidade" %>
<%= ff.input :cv, :label => "Curriculum Vitae" %>
<%= ff.label :area_profissional, :label => "Área Profissional" %>
<%= ff.select :area_profissional, ["Programador_Web", "Programador_Java","Gestor"], :label => "Área Profissional" %>
<%= ff.input :apresentacao, :label => "Apresentação" %>
<%= ff.select :nivel_hab, ["Secundário","Licenciatura","Mestrado","Doutoramento"], :label => "Nível de Habilitações" %>
<%= ff.input :hab_literaria, :label => "Habilitações Literárias" %>
<%= ff.select :situacao_prof, ["Empregado","Desempregado"], :label => "Situação Profissional" %>
<%= ff.input :exp_profissional, :label => "Experiência Profissional" %>
<% end %>
<%= f.submit "Registar", class: "btn btn-large btn-primary" %>
<% end %>
And I can't create the damn User. It keeps rendering the new page. What the hell is wrong.
This is my Controller:
class UsersController < ApplicationController
def new
#user = User.new
if params[:param] == "candidato"
#role = "candidato"
##user.candidato = Candidato.new
#user.build_candidato
else
#role = "entidade"
##user.entidade = Entidade.new
#user.build_entidade
end
end
def create
#user = User.new(user_params)
if user_params[:role] == "candidato"
#user.build_candidato(user_params[:candidato_attributes])
##user.candidato = Candidato.new(user_params[:candidato_attributes])
if #user.save
#Sucesso
redirect_to root_path
else
#Falhou
#role = "candidato"
render 'new'
end
else
##user.entidade = Entidade.new(user_params[:entidade_attributes])
#user.build_entidade(user_params[:entidade_attributes])
if #user.save
#Sucesso
redirect_to root_path
else
#Falhou
#role = "entidade"
render 'new'
end
end
end
private
def user_params
params.require(:user).permit(:role,:email,:nome,:password,:password_confirmation,:pagina,:contacto1,:contacto2,:foto,:cod_postal,:localidade, :candidato_attributes => [:data_nascimento,:bi,:cv,:area_profissional,:apresentacao,:nivel_hab,:hab_literaria,:situacao_prof,:exp_profissional], :entidade_attributes => [:nip,:apresentacao,:atividade])
end
end
If someone knows what's wrong please tell me
Problem is here in your create method you are building dependent object twice and you have has_one relationship. You object for dependent model already created on new action on parent.
Your controller should look like :
def create
#user = User.new(user_params)
if #user.save
redirect_to root_path
else
#role = user_params[:role]
render 'new'
end
end
Form should look like :
<%= simple_form_for #user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.input :email %>
<%= f.input :role, :as => :hidden, :input_html => { :value => #role } %>
<%= f.input :password %>
<%= f.input :password_confirmation, label: "Confirme a password" %>
<%= f.input :nome %>
<%= f.input :foto, :label => "Foto" %>
<%= f.input :cod_postal, :label => "Código-Postal" %>
<%= f.input :localidade %>
<%= f.input :contacto1, :label => "Contactos" %>
<%= f.input :contacto2, label: false %>
<%= f.input :pagina, :label => "Página Pessoal" %>
<% if #role == "candidato" %>
<%= f.fields_for :candidato do |ff| %>
<%= ff.input :data_nascimento, :label => "Data de Nascimento" %>
<%= ff.input :bi, :label => "Bilhete de Identidade" %>
<%= ff.input :cv, :label => "Curriculum Vitae" %>
<%= ff.label :area_profissional, :label => "Área Profissional" %>
<%= ff.select :area_profissional, ["Programador_Web", "Programador_Java","Gestor"], :label => "Área Profissional" %>
<%= ff.input :apresentacao, :label => "Apresentação" %>
<%= ff.select :nivel_hab, ["Secundário","Licenciatura","Mestrado","Doutoramento"], :label => "Nível de Habilitações" %>
<%= ff.input :hab_literaria, :label => "Habilitações Literárias" %>
<%= ff.select :situacao_prof, ["Empregado","Desempregado"], :label => "Situação Profissional" %>
<%= ff.input :exp_profissional, :label => "Experiência Profissional" %>
<% end %>
<%else%>
<%= f.fields_for :entidade do |ff| %>
<%= ff.input :atividade, :label => "atividade" %>
<%= ff.input :apresentacao, :label => "apresentacao" %>
<%= ff.input :nip, :label => "nip" %>
<% end %>
<% end %>
<%= f.submit "Registar", class: "btn btn-large btn-primary" %>
<% end %>
You also have to add :id and _destroy in attributes. It will used at the time of edit and delete child model.
def user_params
params.require(:user).permit(:role,:email,:nome,:password,:password_confirmation,:pagina,:contacto1,:contacto2,:foto,:cod_postal,:localidade, :candidato_attributes => [:id, :data_nascimento,:bi,:cv,:area_profissional,:apresentacao,:nivel_hab,:hab_literaria,:situacao_prof,:exp_profissional, :_destroy], :entidade_attributes => [:id, :nip,:apresentacao,:atividade, :_destroy])
end
#app/controllers/users_controller.rb
Class UsersController < ApplicationController
def new
#user = User.new
#user.send("build_#{params[:param]}")
end
def create
#user = User.new user_params
#user.save
end
private
def user_params
params.require(:user).permit(:role,:email,:nome,:password,:password_confirmation,:pagina,:contacto1,:contacto2,:foto,:cod_postal,:localidade, :candidato_attributes => [:data_nascimento,:bi,:cv,:area_profissional,:apresentacao,:nivel_hab,:hab_literaria,:situacao_prof,:exp_profissional], :entidade_attributes => [:nip,:apresentacao,:atividade])
end
end
When you pass nested attributes, you only need to build the initial associative object
In your create method, you're building the associative data again. A much better way will be to use the code above (albeit edited to represent your redirects), to create the User object
Validations aside, I don't see any reason why the above code wouldn't work with your form
I want to add delete button in actions. but all it does is js goback. I tried <%= f.actions %> didn't show the delete button. below is my effort to add it manually.
<% if can? :update, #parking_branch %>
<%= semantic_form_for #parking_branch do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :parking_company_id, :as => :select, :collection => Hash[ParkingCompany.all.map {|c| [c.company_name,c.id]}], :required => true %>
<%= f.input :branch_name, :required => true %>
<%= f.input :email, :required => true %>
<%= f.input :telephone, :required => false %>
<%= f.input :latitude, :hint =>"Automatically filled based on address" %>
<%= f.input :longitude, :hint =>"Automatically filled based on address" %>
<%= f.input :airport, :required => true %>
<%= f.input :address1 %>
<%= f.input :address2, :required => false %>
<%= f.input :address3, :required => false %>
<%= f.input :city %>
<%= f.input :county %>
<%= f.input :postcode %>
<%= f.input :country, :as => :country, :priority_countries => ["United Kingdom"], :required => true %>
<% end %>
<br />
<%= f.actions do %>
<%= f.action :submit, :button_html => {:class => 'btn-primary', :disable_with => 'Please Wait...' } %>
<%= f.action :cancel, :button_html => {:class => 'btn-danger', :disable_with => 'Please Wait...', :method => :delete } %>
<% end %>
<% end %>
<% else %>
<br />
<h1> You are not authorised to do this! <h1>
<% end %>
I have destroy action in my controller too
first off I'm very new to rails - I'm playing about with a little log in application had it all working and decided to try out simple form - however I can't get my log in form to work with the gem.
Here is what I had and had working;
<h2>Log In</h2>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<p><%= link_to "Forgotten Password?", new_password_reset_path %></p>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log In" %></div>
<% end %>
And here is what I tried to change it to using simple form.
<h2>Log In</h2>
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
This seems to work okay until I try to log in - when I log in it is always displaying my invalid username and password message - I can't figure out where I'm going wrong here. Any help would be much appreciated!
Thanks!
In case 1, you are probably receiving params: { :email => '...', ....} and in case 2, :sessions => { :email => '...', ....}
Check params.inspect
Got it! Many thanks to Zabba for pointing me in the right direction;
My second method works;
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
However I failed to update my controller so where I had;
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
...
end
I had to update to;
def create
user = User.find_by_email(params[:sessions][:email])
if user && user.authenticate(params[:sessions][:password])
...
end
Thanks Zabba!