Rails hit new method on save of simpleform - ruby-on-rails

So i have this method
def create
#newevent = Event.new(create_params)
#newevent.save!
flash[:success] = "Event Created"
redirect_to "/events"
end
And this form
<% provide(:title, "Edit user") %>
<h1>Editing event:
<%= #newevent.id %></h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= simple_form_for #newevent do |f| %>
<div class="form-group">
<%= f.label :eventname %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventname, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :event_type, :collection => ['Concert','Festival','Sports','Theatre'] %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventdesc %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventdesc, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventshortdesc %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventshortdesc, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :pagetitle %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :pagetitle, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :metatag %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :metatag, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventvenuename %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventvenuename, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :time, type: "time", :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.input :date, type: "date", :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :eventimage %>
<div class="row">
<div class="col-md-6">
<%= f.text_field :eventimage, :autofocus => true, class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.check_box :eventready %>
<%= f.label :eventready, "Is event ready for SEO?" %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<%= f.check_box :eventcomplete %>
<%= f.label :eventcomplete, "Is event ready for Public?" %>
</div>
</div>
</div>
<%= f.submit "Save changes", class: "btn btn-info" %>
<%= link_to "Delete", event_path(#newevent), :method => :delete, class: "btn btn-danger" %>
<% end %>
</div>
</div>
I'm currently populating this form with the edit method here
def edit
#newevent = Master.find(params[:id])
end
How can i go about making this pull in information from the Master table to auto populate the table but then save to the Event table?
Sam

What about a simple update method?
def update
#newevent = Event.find(params[:id])
if #newevent.update_attributes(params[:newevent])
redirect_to #user
else
render action: "edit"
end
end

Related

Rails 5: Ways to validate forms

Working on a form:
<%= form_for(#contact, html: {multipart: true}) do |f| %>
<div class="card">
<div class="card-header">
<h2 class="display-5 main-text-blue text-center font-weight-bold">Add New Contact</h2>
</div>
<div class="card-body">
<div class="errors">
<% if #contact.errors.any? %>
<div class="alert alert-danger">
<h5 class="text-center mb-n1 pb-1"><i class="fa fa-exclamation-triangle text-danger"></i> Please correct the following errors: </h5>
</div>
<% end %>
</div>
<div class="form-group row">
<div class="col-md-5 mx-auto">
<div class="wrap" id="avatar-container">
<div class="valign-middle">
<div class="form-group">
<% if #contact.new_record? %>
<%= image_tag "100x100.png", class: "img-responsve img-preview" %>
<% else %>
<%= image_tag #contact.avatar, class: "img-responsve img-preview" %>
<% end %>
<label for="file" class="sr-only">Choose Image</label>
<%= f.file_field :contact_avatar, id: "file" %>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label :name, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :name, class: "form-control #{'is-invalid' if has_error?(#contact, :name) }", id: "name", placeholder: "Name.." %>
<% if has_error?(#contact, :name) %>
<span class="text-danger">
<%= get_error(#contact, :name)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :email, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :email, class: "form-control #{'is-invalid' if has_error?(#contact, :email) }", id: "email", placeholder: "Email.." %>
<% if has_error?(#contact, :email) %>
<span class="text-danger">
<%= get_error(#contact, :email)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :mobile, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :mobile, class: "form-control #{'is-invalid' if has_error?(#contact, :mobile) }", id: "mobile", placeholder: "Mobile.." %>
<% if has_error?(#contact, :mobile) %>
<span class="text-danger">
<%= get_error(#contact, :mobile)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :phone, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :phone, class: "form-control #{'is-invalid' if has_error?(#contact, :phone) }", id: "phone", placeholder: "Phone.." %>
<% if has_error?(#contact, :phone) %>
<span class="text-danger">
<%= get_error(#contact, :phone)%>
</span>
<% end %>
</div>
</div>
<div class="form-group row">
<%= f.label :country, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :country, class: "form-control", id: "country", placeholder: "Country.." %>
</div>
</div>
<div class="form-group row">
<%= f.label :address, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_field :address, class: "form-control", id: "address", placeholder: "Address.." %>
</div>
</div>
<div class="form-row">
<%= f.label :location, class: "col-lg-2 col-form-label" %>
<div class="col-4">
<%= f.text_field :city, class: "form-control", id: "city", placeholder: "City.." %>
</div>
<div class="col-4">
<%= f.text_field :state, class: "form-control", id: "state", placeholder: "State.." %>
</div>
<div class="col-2">
<%= f.text_field :zip, class: "form-control", id: "zip", placeholder: "Zip.." %>
</div>
</div>
<div class="form-group row category-mt">
<%= f.label :category, class: "col-lg-2 col-form-label" %>
<div class="col-lg-5">
<%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Select Category" }, id: "category_select", class: "form-control #{'is-invalid' if has_error?(#contact, :category) }" %>
<% if has_error?(#contact, :category) %>
<span class="text-danger">
<%= get_error(#contact, :category)%>
</span>
<% end %>
</div>
<div class="col-lg-3">
<a class="btn btn-outline-secondary add-category-button btn-block mt-1" href="#" id="add-category-btn">Add Category</a>
</div>
</div>
<div class="form-group row" id="add-new-category">
<label class="col-lg-2 col-form-label" for="location">New Category:</label>
<div class="col-lg-10">
<div class="input-group">
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(#category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(#category, :name) %>
<span class="text-danger">
<%= get_error(#category, :name)%>
</span>
<% end %>
<div class="input-group-append">
<button class="btn btn-outline-secondary category-btn" id="save-new-category-btn" type="button"><i class="fa fa-check"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label :note, class: "col-lg-2 col-form-label" %>
<div class="col-lg-10">
<%= f.text_area :note, class: "form-control", id: "note", placeholder: "Note..", rows: "3" %>
</div>
</div>
</div>
<div class="card-footer">
<%= f.submit "Save", class: "btn btn-primary border-button mb-3 ml-3", id: "save-btn" %>
<a class="btn btn-outline-secondary border-button mt-n3" data-dismiss="modal" href="#" id="cancel-btn">Cancel</a>
</div>
</div>
<% end %>
As you can see here I place some rails validation codes which already exist on contacts model. For instance:
<%= f.text_field :name, class: "form-control #{'is-invalid' if has_error?(#contact, :name) }", id: "name", placeholder: "name.." %>
Which will add the class is-invalid if there are any errors. And this also:
<% if has_error?(#contact, :phone) %>
<span class="text-danger">
<%= get_error(#contact, :phone)%>
</span>
<% end %>
Which will simply print the error text at the bottom if there's an error. Here's the helper codes for that:
def has_error?(resource, field)
resource.errors.messages[field].present?
end
def get_error(resource, field)
msg = resource.errors.messages[field]
field.to_s.capitalize + " " + msg.join(' and ') + '.'
end
So, basically I am working on two tables here: Contact and Category. So far what am I verifying on my form are mostly contacts. I have this feature here where in user can add NEW CATEGORY right on the spot via ajax which is a foreign key on the contacts table which can also be seen on the code above.
Now, on my understanding, I can also add a validation on models. So I tried to put some validation on my category:
validates :name, uniqueness: true
I was hoping that this will validate the uniqueness of the new category being output, meaning if the text (category name) already exist on the database it must prohibit it and throw an error. So what I did is I tried the following code to display the error:
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(#category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(#category, :name) %>
<span class="text-danger">
<%= get_error(#category, :name)%>
</span>
<% end %>
And so I thought it will work however it just throw an error saying
undefined method errors' for nil:NilClass which is I thought it should work since I am using #category but then I realize it's a form for #contact.
Is there a better way to make this work and display the error on the add new category field? I am really stuck on this. I hope someone can help me.
You can get categories errors through f object
<% f.object.categories.each do |category| %>
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(category, :name) %>" placeholder="Enter category name" type="text">
<% if has_error?(category, :name) %>
<span class="text-danger">
<%= get_error(category, :name)%>
</span>
<% end %>
<% end %>

How to upload a file using the file_file_tag?

I want to upload file in form but do not want its field inside a model.
so using the file_filed_tag
at view:
<%= form_tag services_datainterchange_path, method: :post, remote: true do %>
<div class="row">
<div class="col-sm-3 col-md-3">
<div class="form-group">
<%= label_tag :file %>
<%= file_field_tag :file, required: true, class: "form-control", id: "upload_file" %>
</div>
</div>
<div class="col-sm-2 col-md-2">
<div class="form-group">
<%= label_tag :name %>
<%= text_field_tag :name, nil, required: true, class: "form-control", id: "upload_file_name" %>
</div>
</div>
<div class="col-sm-2 col-md-2">
<div class="form-group">
<%= label_tag :source_type %>
<%= select_tag :source_type, options_for_select(ApplicationRecord::SOURCE_TYPE), class: "form-control" %>
</div>
</div>
<div class="col-sm-2 col-md-2">
<div class="form-group">
<%= label_tag :final_type %>
<%= select_tag :final_type, options_for_select(ApplicationRecord::FINAL_TYPE), class: "form-control" %>
</div>
</div>
<div class="col-sm-2 col-md-2">
<div class="form-group">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
At controller:
directory = "public/job_files"
Find.find( directory ) do |fpath|
if FileTest.file?( fpath )
fpath.clone(params[:file])
end
end
But file not upload the parameter do get submit of form.
Thanks
In controller:
name = params['file'].original_filename
uploaded_io = params['file']
File.open(Rails.root.join('public', 'job_files', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end

multiple forms in one view rails

am new to stack overflow..hoping support from all f u
i have employee form with personal details,contact details,salary details etc,arranged,but they are different controllers.i want to get all data in one form in employee view.employee contacts and other forms are not getting saved...help out...
`
<%= form_for(#employee, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |f| %>
<div class="form-group row">
<%= f.label :first_name, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= f.text_field :first_name, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :first_name) %></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= f.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
<div role="tabpanel" class="tab-pane in" id="nav-tabs-0-2">
<%= form_for(#employee.contact_components, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |e| %>
<div class="form-group row">
<%= e.label "P O Box", class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_field :po_box, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :po_box) %></span>
</div>
</div>
<div class="form-group row">
<%= e.label :permanent_address, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_area :permanent_address, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= e.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
</div>`
What you want is a nested form. It allows you to update different model attributes from a single form. That doc link will walk you through each change you need to make to the model, controller and views.

Rails Radio button is only allowing me to select only first option and not the other

What would be the cause of me only being able to select the first radio button, but not the second. Here is what my form looks like.
`
<%= form_for [#item, Calendar.new] do |f| %>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :start_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :end_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
</div>
<div class="form-group">
<div class="btn-group" data-toggle="buttons-radio">
<%= f.collection_radio_buttons :status, Calendar.statuses, :first, :first, checked: Calendar.statuses.first do |b|
b.radio_button + b.label {b.text.split("_").join("").capitalize}
end %>
</div>
</div>
<div class="row new-pricing">
<div class="col-md-10">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">$</span>
<%= f.text_field :item_price, class: "form-control", value: #item.item_price, required: true %>
</div>
</div>
</div>
<div class="col-md-2">
<p style="margin-top: 10px"> Per Day </p>
</div>
</div>
<div class="no-pricing hide">
</div>
<div class="form-group">
<%= f.button "Save", type: :submit, class: "btn btn-success" %>
</div>
<% end %>`
Here is what the html looks like when I click on the "Not available" button. The checked always remains on the first value input button.
The html in the inspection window.
How can I go about fixing this. Sorry if this was an easy question.

Rails: Hide form field when value is popuated

I have this form. When the object has a value for gender, I want to change the gender field to a hidden field, so the user does not need to see it. What is the best way to do this?
<%= form_for( object ,:html => {:class => "form-horizontal"}) do |f| %>
<div class="form-group row">
<%= f.label(:first_name, :class => "control-label col-xs-4 input-lg") %>
<div class="form-inline col-xs-8">
<div class="">
<%= f.text_field( :first_name,{class: "form-control input-lg"}) %>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label(:last_name, :class => "control-label col-xs-4 input-lg") %>
<div class="form-inline col-xs-8">
<div class="">
<%= f.text_field( :last_name,{class: "form-control input-lg"}) %>
</div>
</div>
</div>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<div class="col-xs-8 col-xs-offset-4">
<%= f.submit object.submit_button_name, {class: 'btn btn-lg'} %>
</div>
<% end %>
You could simply use an if statement to render different field types based on whether gender is set or not:
<% if object.gender %>
<%= f.hidden_field(:gender)
<% else %>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<% end %>
Instead of hidding it, it's better to not rendering it. Try it like this:
<% unless object.gender != nil %>
<div class="form-group row">
<%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
<%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
</div>
</div>
<% end %>

Resources