Form helper collection_check_boxes returns data as an array, but how to put it into mysql database (or into variable in the controller)?
In my controller I have
def new
#subjects = Subject.all
#student = Student.new(:first_name => "First name", :last_name => "Last name", :birthdate => "Birthday", :email => "E-mail", :phone_number => "Phone number", :classes)
end
def create
#student = Student.new(student_params)
if #student.save
redirect_to(students_path)
else
render('new')
end
end
private
def student_params
params.require(:student).permit(:first_name, :last_name, :birthdate, :email, :phone_number, :classes)
end
Form looks like this:
<%= form_for(#student) do |f| %>
<%= f.collection_check_boxes :classes, Subject.all, :id, :subject %>
<%= f.submit("Create student") %>
<% end %>
Related
This is my code
from index.html.erb
<%= form_for index_path, :method => "POST" do %>
<%= label_tag :name, "Enter your full name: " %>
<%= text_field_tag :name, #name, :placeholder => "Enter your name" %><br/>
<%= label_tag :email, "Enter your e-mail address: " %>
<%= email_field_tag :email, #email, :placeholder => "something#domain.com" %><br/>
<%= label_tag :question, "Type your question: " %>
<%= text_area_tag :question, #question, :size => "30x5", :placeholder => "Your text goes here", :maxlength => "130" %><br/>
<%= submit_tag "Ask Question" %>
from Blog Controller
class BlogController < ApplicationController
def index
#name = params['name']
#email = params['email']
#question = params['question']
end
def about
end
def contact
end
end
And from seeds.rb:
Invitation.create(:name => "#{#name}", :email => "#{#email}", :question => "#{#question}")
When I fill all fields and submit, it creates blank record in database, what am I doing wrong?
You are doing it wrong. You can't access the instance variables that are defined in the controller inside seeds.rb. Instead you just need to create the record inside the index action
def index
#name = params['name']
#email = params['email']
#question = params['question']
Invitation.create(:name => #name, :email => #email, :question => #question)
end
seeds.rb is used to create records with default values to seed the database. Normally these values are hardcoded.
I'm using nested attributes to user adresses.
class User < ApplicationRecord
accepts_nested_attributes_for :address, update_only: :true
I'm passing the address id as hidden field at sign-in.
<%= form_for (#user), :html => {:class => 'lead-form'} do |f| %>
<%= f.email_field :email, class:'form-control', placeholder: "Email", required: true %>
<%= f.password_field :password, class:'form-control', placeholder: "Senha", required: true %>
<%= f.fields_for :address do |addresses_form| %>
<%= addresses_form.hidden_field :address_id, :value => #address.id %>
<% end %>
<%= f.submit 'Pedir meu convite', class: "button lead-form-button" %>
<% end %>
The e-mail is validate as uniqueness. So, when somebody try to sign in with an already taken e-mail the address params are passed as nil and i get the error:
NoMethodError in Users#create
undefined method `id' for nil:NilClass
Any idea how can i fix that?
Here is my controller:
def new
#user = User.new
#address = #user.build_address
end
def create
#user = User.new(user_params)
if #user.save
log_in #user
flash[:success] = "Você está dentro. Esse é seu perfil! =) "
redirect_to #user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:id, :primeiro_nome, :sobrenome, :email, :cpf, :telefone, :sexo, :data_de_nascimento, :password, address_attributes: [:id, :logradouro, :bairro, :cidade, :uf, :complemento, :cep])
end
ps: The association is one to one. How can i call the address attributes if i don't pass as hidden.
Address belongs to user? If yes, you just call #user.address
Maybe you should try using try method:
<%= addresses_form.hidden_field :address_id, :value => #address.try(:id) %>
I have user and user has one address. I need to create a form which is like edit profile page. Which will save few user's fields and save user's address. But everytime while I submit form, user's data is correctly being saved in DB, but for address, new record is being created every time with correct user_id value and other field value is null.
Query: How to save address in database?
class User < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :user
belongs_to :country
attr_accessor :city, :zip_code, :street_address, :state
end
Form
<%= form_for #user, url: {controller: :profiles, action: :update} do |f| %>
<%= f.text_field :first_name, autofocus: true, :placeholder => 'First Name' %>
<%= f.text_field :last_name, :placeholder => 'Last Name' %>
<%= f.fields_for :address do |address_fields| %>
<%= address_fields.select :country_id, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],:disabled => ["Select One"]) %>
<%= address_fields.select :state, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],:disabled => ["Select One"]) %>
<%= address_fields.text_field :city, :placeholder => 'City' %>
<%= address_fields.text_area :street_address, :placeholder => 'Street Address' %>
<%= address_fields.text_field :zip_code, :placeholder => 'Zip Code' %>
<% end %>
<%= f.submit "Update", :class => 'btn btn-primary' %>
controller
class ProfilesController < ApplicationController
def edit
#user = current_user
#user.build_address if #user.address.nil?
end
def update
#user = current_user
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to welcome_url, notice: 'Your Profile was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
private
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :phone_number, :date_of_birth, address_attributes: [ :id, :city, :country_id, :zip_code, :street_address, :state])
end
end
Your country_id select field will return a string, while your Address model probably expects an integer. This might prevent the record from being saved correctly?
What is the proper way to have a form in a partial view reference an empty model in order to handle validation properly as defined on the model. Should I instantiate a new, empty model in the partial view and pass it through to the form? Here is what I'm working with...
MODEL
class NewsletterSignup < ActiveRecord::Base
def self.columns()
#columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = false)
columns << ActiveRecord::ConnectionAdapters::Column.new(name, default, sql_type, null)
end
def persisted?
false
end
column :first_name, :string
column :last_name, :string
column :email, :string
column :zip, :string
validates :first_name, :last_name, :email, :zip, :presence => true
validates :email, :format => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates :zip, :format => /^\d{5}$/
end
Partial View
<%
signup = newsletter_signup.new
%>
<%= simple_form_for(signup, ...) do |f| %>
<%= f.input :first_name, :label => "First Name:" %>
<%= f.input :last_name, :label => "Last Name:" %>
<%= f.input :email, :label => "Email:" %>
<%= f.input :zip, :label => "Zip:" %>
...
<% end %>
But I can't seem to instantiate a new model like this. I assume I have to reference it in the view. (Note, I'm new to rails but have a decade+ of professional software development experience, so I apologize if some of rails constructs seem foreign to me and I may just be overlooking something simple!)
If your controller looks like this
def new
Model.new
end
def create
#model = Model.new(params[:model])
if #model.save
redirect root_path, notice: "Saved"
else
render action: 'new'
end
end
def edit
Model.find(params[:id])
end
def update
if #model.update(params[:model])
redirect root_path, notice: "Updated"
else
render action: 'edit'
end
end
You can have views like:
# new.html.erb
<%= render 'form' %>
# edit.html.erb
<%= render 'form' %>
# _form.html.erb
<%= form_for #model do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
...
<% end %>
Bassicaly my problem what to do if i have 3 forms and one submit button.
I want to create a form which sends email to each recipient and then create new record in free_registration_coupons table.
I need validation of email for this form.
Model FreeRegistrationCoupon: recipient_email, token, sender_id
For now i have this:
class FreeRegistrationCouponsController < ApplicationController
def send_invitations
emails = [params[:recipient_email_1], params[:recipient_email_2], params[:recipient_email_3]]
emails.reject!{ |e| e.eql?("") }
if emails.present?
emails.each do |e|
FreeRegistrationCoupon.create(:recipient_email => e, :sender_id => current_user.id)
#MAILER
end
redirect_to root_path, :notice => "You just send #{emails.size} invitations!"
else
redirect_to(:back)
end
end
end
class FreeRegistrationCoupon < ActiveRecord::Base
before_save :generate_token
attr_accessor :recipient_email, :sender_id
validates :recipient_email, :presence => true, :email => true
def generate_token
self.token = SecureRandom.hex
end
end
This is form which is in other controller CarsController#confirm:
<%= form_tag :controller => 'free_registration_coupons', :action => "send_invitations" do %>
<!-- errors -->
<%= label_tag :recipient_email_1 %>
<%= text_field_tag :recipient_email_1 %>
<%= label_tag :recipient_email_2 %>
<%= text_field_tag :recipient_email_2 %>
<%= label_tag :recipient_email_3 %>
<%= text_field_tag :recipient_email_3 %>
<%= submit_tag %>
<% end %>
I think you should have defined your form using:
<%= form_tag :controller => 'free_registration_coupons', :action => "send_invitations" do %>
<%= #error_message %>
<%= label_tag "recipient_email[1]" %>
<%= text_field_tag "recipient_email[1]" %>
<%= label_tag "recipient_email[2]" %>
<%= text_field_tag "recipient_email[2]" %>
<%= label_tag "recipient_email[3]" %>
<%= text_field_tag "recipient_email[3]" %>
<%= submit_tag %>
<% end %>
This way it will be easier to treat all email address on your controller and you can track those errors to display them afterwards:
class FreeRegistrationCouponsController < ApplicationController
def send_invitations
emails = params[:recipient_email]
emails.reject!{ |param, value| value.eql?("") }
errors = []
if emails.any?
emails.each do |param, value|
validation_result = FreeRegistrationCoupon.save(:recipient_email => value, :sender_id => current_user.id)
#MAILER
end
redirect_to root_path, :notice => "You just send #{emails.size} invitations!"
else
#error_message = "You have to include, at least, one e-mail address!"
render :name_of_the_action_that_called_send_invitations
end
end
end
I didnt test this code. Hope it helps!