I have a customer model that belongs to user. However, when creating the new customer, the user will not be signed in. Instead I will be assigning the user based on his name attribute.
Here is the customer migration:
create_table :customers do |t|
t.belongs_to :user
t.string :name, null: false
t.date :install_date, null: false
t.decimal :mmr, null: false
t.boolean :sixty_month, default: false
t.boolean :eft, default: false
t.boolean :activation, default: false
t.timestamps
end
I then have a new customer form:
<h1>Create Customer</h1>
<%= form_for #customer do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :install_date %>
<%= f.date_field :install_date %>
</div>
<div>
<%= f.label :mmr %>
<%= f.number_field :mmr %>
</div>
<div>
<%= f.label :sixty_month %>
<%= f.check_box :sixty_month %>
</div>
<div>
<%= f.label :eft %>
<%= f.check_box :eft %>
</div>
<div>
<%= f.label :activation %>
<%= f.check_box :activation %>
</div>
<div>
<%= f.label :users_name %>
<%= f.text_field :users_name %>
</div>
<%= f.submit 'Create Customer' %>
<% end %>
I need to get the users_name field and match it with the associated user and take that users id and assign it to the customers :user_id attribute. In the customers controllers create action so far I have:
#customer.user_id = User.where("name = #{}").id
I need to pull to input from the users_name field in the new customer form and put it into the above code somehow. I just don't know how haha. All help is appreciated!
You will probably want to move this out of the controller and into a helper method but you could do like this:
def create
#customer = Customer.new(customer_params)
if params[:users_name]
user = User.find_by(name: params[:users_name])
#customer.user_id = user.id unless user.nil?
end
if #customer.save
flash[:success] = "Customer created!"
redirect_to #customer
else
render 'new'
end
end
This is assuming you are using Rails 4.
Related
Rails noob here, how do I get a Rails form to save a boolean checkbox to postgres? I've tried dozens of things I've found online and none of it is working. No matter what I do, the entry in the Postgres DB is "Null". Here is my
Model(schema):
ActiveRecord::Schema.define(version: 20171227200151) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "notes", force: :cascade do |t|
t.string "note_title"
t.string "note_text"
t.boolean "note_scratch", default: false, null: false
t.boolean "note_info"
t.boolean "note_reminder"
t.boolean "note_task"
t.string "note_tags"
t.date "note_remind_date"
t.time "note_remind_time"
t.integer "note_archived"
t.date "note_archived_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
view:
<%= form_with scope: :note, url: notes_path, local: true do |f| %>
<p>
<%= f.label :note_title %><br>
<%= f.text_field :note_title %>
</p>
<p>
<%= f.label :note_text %><br>
<%= f.text_area :note_text %>
</p>
<p>
<%= f.label :notes_scratch, "Scratch" %>
<%= f.check_box :notes_scratch %>
<%= f.label :notes_info, "Info" %>
<%= f.check_box :notes_info %>
<%= f.label :notes_reminder, "Reminder" %>
<%= f.check_box :notes_reminder %>
<%= f.label :notes_task, "Task" %>
<%= f.check_box :notes_task %>
</p>
<p>
<%= f.label :note_tags %><br>
<%= f.text_field :note_tags %>
</p>
<p>
<%= f.label :note_remind_date %><br>
<%= f.date_field :note_remind_date %>
</p>
<p>
<%= f.label :note_remind_time %><br>
<%= f.time_field :note_remind_time %>
</p>
<%= f.submit %>
<% end %>
Controller:
class NotesController < ApplicationController
def new
end
def create
# for testing
# render plain: params[:note].inspect
#note = Note.new(note_params)
#note.save
redirect_to #note
end
private
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
end
If I just render the form results, it will show checked boxes as 1s, but it always comes up Null in the database, even if I set the datatype to Integer (hoping it would store the 1). All of the other fields record just fine. It's only the checkboxes that don't work.
I appreciate any assistance you can lend!
-Brick
Review your note_params (and schema.rb):
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
In your view: notes_* needs to be singular as your schema is note_*.
I have two models: Entreprise & Prospect
create_table "entreprises", force: :cascade do |t|
t.string "name"
t.text "adresse"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "siret"
t.string "numtva"
t.string "tel"
t.integer "prospect_id"
end
create_table "prospects", force: :cascade do |t|
t.string "full_name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "entreprise_id"
end
Prospects belongs_to entreprises, and Entreprise has_many Prospects.
-
My issue :
I have two buttons : Create a new entreprise or reate a new prospect.
I have two way to create a new prospect :
Under my entreprise because an entreprise has_many prospects
Directly from navbar with "create prospect" button
So I have two forms :
If we click on "create prospect" (create method in my controller):
<%= form_for :prospects, url: prospects_path do |f| %>
<p>
<%= f.label :entreprises %><br><br>
<%= f.collection_select :entreprise_id, Entreprise.order(:name),:id,:name, include_blank: true %>
</p><br>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
If we create prospect under entreprise ("directcreate" method in my controller) :
<%= form_for([#entreprise, #entreprise.prospects.build]) do |f| %>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
My Controller for Prospects :
class ProspectsController < ApplicationController
def index
#prospects = Prospect.all.limit(15).order('created_at DESC')
end
def new
#prospect = Prospect.new
end
def directcreate
#prospect = Prospect.new(entreprise_params)
if #prospect.save
redirect_to #prospect
else
render 'new'
end
end
def create
#entreprise = Entreprise.find(params[:entreprise_id])
#prospect = #entreprise.prospects.create(params[:prospect].permit(:full_name, :email))
redirect_to entreprise_path(#entreprise)
end
def destroy
#entreprise = Entreprise.find(params[:entreprise_id])
#prospect = #entreprise.prospects.find(params[:id])
#prospect.destroy
redirect_to entreprise_path(#entreprise)
end
end
My problème is that the "create" method in my controller is the only one who works.. and not "directcreate" method.
--
QUESTION : How can I do to choose the controller I want according to the form ?
My question is probably not well ask.
FYI I try this in my new.html.erb :
<%= render 'formdirect', :controller => "directcreate" %>
but it doesn't work.
You can try something like this. You just have to explicitly choose your controller action.
<%= form_for([#entreprise, #entreprise.prospects.build], :url => { :action => : directcreate }) do |f| %>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
I noob in Rails. I use rails 4.2.4. I have a problem with my contact form. I have an error: First argument in form cannot contain nil or be empty on the 3 line of this code (contact_form.html.erb):
<div class="all-banners-width">
<figure class="green-background">
<%= form_for #contact_form do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</figure>
</div>
My contact_form_controller.rb is:
class ContactFormController < ApplicationController
def contact_form
#contact_form = ContactForm.new
end
def create
#contact_form = ContactForm.new(contact_form_params)
#contact_form.save
redirect_to root_path, notice: "Saved"
end
private
def contact_form_params
params.require(:contact_form).permit(:name, :phone, :email, :text)
end
end
My DB migration file is:
class CreateContactForms < ActiveRecord::Migration
def change
create_table :contact_forms do |t|
t.string :name
t.string :phone
t.string :email
t.string :text
t.timestamps null: false
end
end
end
If I change 3rd line from contact_form.html.erb to the next:
<%= form_for :contact_form do |f| %>
then I have not an error, but anyway contact form does not work and there are not any errors I can see.
Can somebody help me? I have not idea, where is problem.
You miss action new in controller:
def new
#contact_form = ContactForm.new
end
your template is rendered with #contact_form = nil.
I have a many-many relationship with students and organizations. When creating a new student, I want to have a checkbox to select one or more organizations and save that. How do I do this? What does the MVC look like specifically? I can't find any online resource that gives me the whole overview.
Updated code:
in my form partial:
<%= simple_form_for (#student) do |f| %>
<div class="field">
<%= f.label :organization_ids %><br />
<%= collection_check_boxes :student, :organization_ids, Organization.all, :id, :name %>
</div>
<div class="actions">
<button class="btn btn-primary" type="submit">Save</button> <%= link_to "Cancel", :back, {:class=>"btn btn-primary"} %>
</div>
<% end %>
controller:
def update
#student = Student.find(params[:id])
if Student.save_existing(params[:id], params[:student])
flash[:notice] = "Student was successfully updated!"
redirect_to students_path
else
flash[:error] = "Student failed to update."
redirect_to students_path
end
end
def student_params
params.require(:student).permit(:name, :organization_ids => [])
end
student table:
create_table "students", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "organization_id"
t.integer "organization_ids"
end
my problem: when i do #student.organization_ids.inspect, it gives me an empty array meaning that the form didn't save my input form checkboxes
You can use collection_check_boxes. Something like below in your student create form would do.
<div class="field">
<%= f.label :organization_ids %><br />
<%= collection_check_boxes(:student, :organization_ids, Organization.all, :id, :name) %>
</div>
I'm trying to insert a drop down menu values to a table named units and that drop values are coming from another model named properties.
Here is the schema of the units.
create_table "units", :force => true do |t|
t.string "number"
t.decimal "monthly_rent"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "property_id"
end
on my view/units/new.html.erb I have this.
<% form_for #unit do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :property_id %>
<br/>
<%= select (:unit, :property_id, Property.all.collect {|property| [property.name,property.id,]}) %>
</p>
<p>
<%= f.label :number %>
<br/>
<%= f.text_field :number %>
</p>
<p>
<%= f.label :monthly_rent %>
<br/>
<%= f.text_field :monthly_rent %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
And here is my controller method
def create
#unit = Unit.new(params[:unit])
# #unit.property_id = 1
if #unit.save
flash[:notice] = "Successfully created unit."
redirect_to #unit
else
render :action => 'new'
end
end
Only number and monthy_rent getting inserted to the table but the property_id doesn't come. Can some body help me on this please? Thanks
The possible issue I can see from what you've shown us is the errant comma in your select helper. Try rewriting it like this (also using the form helper method):
<%= f.select :property_id, Property.all.collect {|property| [ property.name, property.id ] } %>
And yes, the output of the create params to your log would help immensely.
I figured it out, sorry guys it was my mistake. It was not working because, property_id is not added to the attr_accessible, once I added that it worked. but I still don't know the exact issue yet.
class Unit < ActiveRecord::Base
belongs_to :property
attr_accessible :number, :monthly_rent, :property_id
end