How to update unique fields using a confirm page? - ruby-on-rails

So I have this book edit page which contains unique fields(Title, ISBN). But before I submit the form to update, I need to send first the form information to another confirmation page and then update. But when I pass the form info to the confirmation page, the unique validations for Title and ISBN fails. I have this code:
book.rb
validates :title, presence: true,
uniqueness: true
validates :isbn, presence: true,
uniqueness: true,
format: { with: /[0-9]/ }
edit.html.erb
<%= form_for #book, url: confirm_edit_admin_book_path, method: :patch, html: { class: "form" } do |f| %>
<%= f.hidden_field :id %>
<div class="float-right">
<button class="btn btn-primary">Confirm</button>
</div>
<h4 class="card-title">Edit Book</h4>
<div class="form-group row mt-5">
<%= f.label :title, class: "col-md-2 col-form-label required" %>
<div class="col-md-10">
<%= f.text_field :title, class: "form-control" %>
<%= render "admin/shared/error_field", field: :title %>
</div>
</div>
<div class="form-group row">
<%= f.label :isbn, "ISBN", class: "col-md-2 col-form-label required" %>
<div class="col-md-10">
<%= f.text_field :isbn, class: 'col-sm-12 form-control' %>
<%= render "admin/shared/error_field", field: :isbn %>
</div>
</div>
<div class="form-group row">
<%= f.label :released_at, class: "col-md-2 col-form-label required" %>
<div class="col-md-10">
<%= f.date_field :released_at, class: 'col-sm-12 form-control' %>
<%= render "admin/shared/error_field", field: :released_at %>
</div>
</div>
<div class="form-group row">
<%= f.label :description, class: "col-md-2 col-form-label" %>
<div class="col-md-10">
<%= f.text_area :description, class: 'col-sm-12 form-control' %>
<%= render "admin/shared/error_field", field: :description %>
</div>
</div>
<div class="form-group row">
<%= f.label :quantity, class: "col-md-2 col-form-label required" %>
<div class="col-md-10">
<%= f.number_field :quantity, class: 'col-sm-12 form-control' %>
<%= render "admin/shared/error_field", field: :quantity %>
</div>
</div>
<div class="float-right">
<%= f.submit "Confirm", class: "btn cur-p btn-primary" %>
</div>
<% end %>
books_controller.rb
def confirm_edit
#book = Book.new(book_params)
book_info = Book.find(#book.id)
if #book.valid?
session[:book_update] = #book
else
render 'edit'
end
end
def update
#book = Book.find(params[:id])
#book.update_attributes(session[:book_update].compact)
session.delete(:book_update)
redirect_to admin_book_url
end

In the confirm_edit action you are creating a new instance of Book using the params from the update action, which are presumably the params from an existing book.
#book = Book.new(book_params)
You are then calling #book.valid? which is going to fail (unless you've changed the Title and ISBN) because a book with the same values is already in the database.
You could retrieve the book from the database and then use .assign_attributes to check validity that way perhaps?
def confirm_edit
#book = Book.find(params[:id])
#book.assign_attributes(book_params)
if #book.valid?
session[:book_update] = #book
else
render 'edit'
end
end

Related

rails 6 save data to multiple tables from one controller

I'm still really new to rails. I have a site that allows tournament directors to host online registration. When they setup the registraiton form they can add custom fields for different section like basic info, division, etc. That part is working but when the user goes to the registraiton form, I am able to pull the fields into the form but I don't know how to save them to a separate table. I have a participants table and the custom fields are in a fields table.
When a user registers or edits their registraion info, how do I save the input for the custom fields into a separate table. I have a table called field_answers with participant_id, field_id and answer, but can't figure out how to save the answers from the participants_controler.
Below is the form for the basic section of the registraiton form. The #basic.each section is where the custom fields are being pulled in, and I'm guessing I'll need to give the fields unique names rather than just answer then save them but I don't know how to do that.
<%= form_with model: [#event, #participant], class: "shadow p-3 mb-3 rounded text-dark", local: true do |f| %>
<%= f.hidden_field :child_id, value: #cid %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :event_id, value: #tID %>
<%= f.hidden_field :basic_complete, value: 1 %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :first_name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :first_name, class: 'form-control', value: #part.first_name, placeholder: "First name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :last_name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :last_name, class: 'form-control', value: #part.last_name, placeholder: "Last name" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :date_of_birth %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.date_field :dob, class: 'form-control', value: #part.dob, placeholder: "Date of Birth" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :gender %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<label class="main">Female
<%= f.radio_button :gender, "female", checked: true if #part.gender == "female" %>
<span class="w3docs"></span>
</label>
<label class="main">Male
<%= f.radio_button :gender, "male", checked: true if #part.gender == "male" %>
<span class="w3docs"></span>
</label>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :address1 %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :address1, class: 'form-control', value: #part.address1, placeholder: "Address 1" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :address2 %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :address2, class: 'form-control', value: #part.address2, placeholder: "Address 2" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :city %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :city, class: 'form-control', value: #part.city, placeholder: "City" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :state %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :state, class: 'form-control', value: #part.state, placeholder: "State" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :country %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :country, class: 'form-control', value: #part.country, placeholder: "Country" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :zip %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :zip, class: 'form-control', pattern: "[0-9]*", value: #part.zip, placeholder: "Postal Code" %>
</div>
</div>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label :phone %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :phone, class: 'form-control', pattern: "[0-9]{3}-[0-9]{3}-[0-9]{4}", maxlength: "12", value: #part.phone, placeholder: "Phone" %>
</div>
</div>
<% #basic.each do |b| %>
<div class="form-group row">
<div class="col-md-2 col-sm-12 col-form-label">
<%= f.label b.name %>:<span class="text-danger">*</span>
</div>
<div class="col-md-10 col-sm-12">
<%= f.text_field :answer, class: 'form-control' %>
<%= f.hidden_field :field_id, value: b.id %>
<%= f.hidden_field :event_id, value: #tID %>
</div>
</div>
<% end %>
<div class="form-group row">
<div class="col-md-8 col-sm-12"></div>
<div class="col-md-4 col-sm-12">
<%= f.submit "Register", class: "profile_btn" %>
</div>
</div>
<% end %>
Here is my participants_controller.rb:
class ParticipantsController < ApplicationController
before_action :event
before_action :participant, only: %i[create]
def new
#participant = #event.participants.new
if current_user
#user = User.find(current_user.id)
#children = #user.children
end
#basic = Field.where(event_id: #event.id, field_type: 'basic')
end
def create
byebug
if #participant.save
flash[:success] = 'Child was successfully created.'
#redirect_to edit_event_participant_path(#participant.id)
redirect_to "/events/#{#event.id}/participants/#{#participant.id}/edit?rt=#{#participant.child_id != nil ? 'child' : 'self'}&step=2"
else
byebug
flash[:notice] = #participant.errors.full_messages.join('<br />').html_safe
redirect_back(fallback_location: events_path)
end
end
def show
end
def edit
#participant = Participant.find(params[:id])
#child = Child.find(#participant.child_id)
#custom_basic = Field.find_by(event_id: #participant.event_id, field_type: 'basic')
end
def update
end
private
def event
#event = Event.find_by(id: params[:event_tourn_url])
end
def participant
#participant = event.participants.new(participant_params)
end
def participant_params
params.require(:participant).permit(:event_id, :user_id, :child_id, :first_name, :last_name, :dob, :gender, :address1, :address2, :city, :state, :country, :zip, :phone, :basic_complete)
end
end
I'd appreciate any help.
Edit:
I edited my participant_params in the controller to:
def participant_params
params.require(:participant).permit(:event_id, :user_id, :child_id, :first_name, :last_name, :dob, :gender,
:address1, :address2, :city, :state, :country, :zip, :phone,
participant_fields_attributes: [:id, :participant_id, :field_id, :answer])
end
And my participants model is:
class Participant < ApplicationRecord
belongs_to :event
has_many :participant_fields
accepts_nested_attributes_for :participant_fields
When I submit all the correct fields are added to the participants table but nothing to the participant_fields table. And in the logs I get Unpermitted
parameters: :answer, :field_id

Duplicating a record in Rails except for one attribute?

In my Rails application I am getting the duplication of last Expense record after the creation of new record but I want to exclude one attribute while duplication that is :description_other, " ".
expenses_controller.rb
class ExpensesController < ApplicationController
# GET /expenses/new
def new
if Expense.last.present?
#expense = Expense.last.dup
else
#expense = Expense.new
end
end
def expense_params
params.require(:expense).permit(:date, :description, :description_other, :trips, :fare, :credit)
end
end
_form.html.erb
<div class="row">
<div class="col-md-3 col-md-offset-4">
<%= form_for(#expense) do |f| %>
<div class="form-group">
<%= f.label :date %><br>
<span class="datetime"><%= f.date_select :date %></span>
</div>
<div class="form-group">
<%= f.label :credit %><br>
<%= f.text_field :credit, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.select(:description, options_for_select([['PLEASE SELECT...', ''],['METRO', 'METRO'], ['BUS', 'BUS'], ['TAXI', 'TAXI'], ['OTHERS', 'OTHERS'], ['FOOTLOOSE', 'FOOTLOOSE']]), {}, {class: "form-control", id: "expense_description"}) %>
<br>
<div id="otherDesc">
<%= f.text_field :description_other, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :trips %><br>
<%= f.number_field :trips, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :fare %><br>
<%= f.text_field :fare, class: "form-control" %>
</div>
<br>
<div style="margin-left: 70px;" class="form-group">
<%= f.submit #expense.new_record? ? "Create Expense" : "Update Expense", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
</div>
</div>
I have tried but unable to get the desired result.
Any suggestions are most welcome.
Thank you in advance.
You should add
<div id="otherDesc">
<%= f.text_field :description_other, class: "form-control", value: " "%>
</div>
use tap and then exclude property by setting to nil
def new
if Expense.last.present?
# exclude here
#expense = Expense.last.dup.tap do | exp |
exp.description_other = nil
end
else
#expense = Expense.new
end
end

rails nested form for many to many not working as expected

I have 3 models,
the relationship between those models is many to many relationship.
unit model
has_many :unitrentperiods, inverse_of: :unit
has_many :rentperiods, :through => :unitrentperiods
accepts_nested_attributes_for :unitrentperiods
unitrentperiod model
belongs_to :unit
belongs_to :rentperiod
rentperiod model
has_many :unitrentperiods, inverse_of: :rentperiod
has_many :units, :through => :unitrentperiods
and i have a unit form, where i can create a new unit or edit an unit.
i want to make my unit form not just able to manage my unit but able to manage my "unitrentperiod" as well, so lets say i have 2 rent periods like
"6 months"
"12 months"
i want my unit form to display those rent periods also with price input, so when i save my unit form, i also save 2 unitrentperiod based on rent periods in database, i did this in my view:
<style>
.divCB {
margin-right: 10px
}
</style>
<%= form_for(#unit) do |f| %>
<% if #unit.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#unit.errors.count, "error") %> prohibited this unit from being saved:</h2>
<ul>
<% #unit.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
<div class="page-header">
<div class="row">
<div class="col-md-7">
<h1 class="h2 page-title">Manage Unit</h1>
<div class="text-muted page-desc"></div>
</div>
<div class="col-md-5 charts">
<div class="row">
</div>
</div>
</div>
</div>
<!-- Breadcrumb -->
<ol class="breadcrumb">
<li class="active">Manage Unit</li>
<!-- Breadcrumb Menu-->
</ol>
<div class="container-fluid">
<div class="animated fadeIn">
<!--/.row-->
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<strong>Unit Form</strong>
</div>
<%= form_for #unit, html: {class: "form-horizontal"} do |f| %>
<div class="card-block">
<div class="form-group row">
<%= f.label 'Apartement', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :apt_id, #apts, :id, :apt_name, {:prompt => "Please select an Apartement"}, {:id => 'apartements_select', :class => "select", :style => "width:240px"}) %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Tower', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :tower_id, #towers, :id, :tower_name, {:prompt => "Select a Tower"}, {:id => 'towers_select', :class => "select", :style => "width:240px"}) %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit number', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitno, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit size', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitsize, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit view', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :unitview, options_for_select(#unit_views.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.unitview), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Room Type', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= collection_select(:unit, :room_type_id, #roomtypes, :id, :room_type_desc, {:prompt => false}, class: "form-control") %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit floor', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitfloor, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit description', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :unitdesc, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Total bed', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :totalbedroom, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Furnish', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :furnish, options_for_select(#furnish_types.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.furnish), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'For sell', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :isforsell, options_for_select( [ ['No', 0], ['Yes', 1] ], selected: #unit.isforsell), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Sell price', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.text_field :sellprice, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'For rent', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :isforrent, options_for_select( [ ['No', 0], ['Yes', 1] ], selected: #unit.isforrent), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Rent price', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<table>
<% #rentperiods.each do |rentperiod| -%>
<tr>
<td><%= rentperiod.rentmonth %> Month</td>
<td>
<%= fields_for(#unitrentperiods) do |elem| %>
<%= elem.text_field :rentprice, class: 'col-md-4 form-control' %> IDR
<%= elem.hidden_field :rentperiod_id, :value => rentperiod.id %>
<% end %>
</td>
</tr>
<% end -%>
</table>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit status', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :unitstatus, options_for_select(#unit_statuses.collect { |s| [s[0].humanize, s[0]] }, selected: #unit.unitstatus), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Photo Template', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<%= f.select :template_id, options_for_select(#templates.collect { |s| [s[:type], s[:id]] }, selected: #unit.template_id), {} , class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= f.label 'Floor plan', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<div id="divFloorplan">
</div>
</div>
</div>
<div class="form-group row">
<%= f.label 'Unit plan', class: 'col-md-4 form-control-label' %>
<div class="col-md-8">
<div id="divUnitplan">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-dot-circle-o"></i> Submit</button>
</div>
<% end %>
</div>
</div>
<!--/col-->
</div>
<!--/.row-->
</div>
</div>
<script>
$(document).ready(function()
{
$('.select').select2();
$('#apartements_select').change(function() {
$.ajax({
url: "/units/gettowers",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
$.ajax({
url: "/units/getfloorplans",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
$.ajax({
url: "/units/getunitplans",
data: {
apt_id : $('#apartements_select').val()
},
dataType: "script"
});
});
});
</script>
controller
class UnitsController < ApplicationController
before_action :set_unit, only: [:show, :edit, :update, :destroy]
# GET /units
# GET /units.json
def index
#units = Unit.paginate(page: params[:page])
end
# GET /units/1
# GET /units/1.json
def show
end
def gettowers
# updates towers based on apartement selection
tower = Tower.where(:apt_id => params[:apt_id])
# map to name and id for use in our options_for_select
#towers = tower.map{|a| [a.tower_name, a.id]}.insert(0, "Select a Tower")
end
def getfloorplans
floorplans = Floorplan.where(:apt_id => params[:apt_id]).select("id, floorplanphoto")
#imgs = floorplans
end
def getunitplans
unitplans = Unitplan.where(:apt_id => params[:apt_id]).select("id, unitplanphoto")
#imgs = unitplans
end
def preparedata
#master tables
#apts = Apt.all
#towers = Tower.all
#roomtypes = RoomType.all
#rentperiods = Rentperiod.all
array = []
Template.all.each do |t|
hash = { :type => t.room_type.room_type_desc, :id => t.id }
array.push(hash)
end
#templates = array
#enum from model
#unit_views = Unit.unitviews
#furnish_types = Unit.furnishes
#unit_statuses = Unit.unitstatuses
end
# GET /units/new
def new
#unit = Unit.new
#unitrentperiods = #unit.unitrentperiods.build
preparedata
# #rentperiods.each do |r|
# #unit.unitrentperiods.build
# end
end
# GET /units/1/edit
def edit
##unitrentperiods = #unit.unitrentperiods
#unitrentperiods = #unit.unitrentperiods.build
preparedata
end
# POST /units
# POST /units.json
def create
#unit = Unit.new(unit_params)
respond_to do |format|
if #unit.save
format.html { redirect_to #unit, notice: 'Unit was successfully created.' }
format.json { render :show, status: :created, location: #unit }
else
format.html { render :new }
format.json { render json: #unit.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /units/1
# PATCH/PUT /units/1.json
def update
respond_to do |format|
if #unit.update(unit_params)
format.html { redirect_to #unit, notice: 'Unit was successfully updated.' }
format.json { render :show, status: :ok, location: #unit }
else
format.html { render :edit }
format.json { render json: #unit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /units/1
# DELETE /units/1.json
def destroy
#unit.destroy
respond_to do |format|
format.html { redirect_to units_url, notice: 'Unit was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_unit
#unit = Unit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def unit_params
params.require(:unit).permit(:apt_id, :tower_id, :unitno, :unitsize, :unitview,
:room_type_id, :unitfloor,
:unitdesc, :isforsell, :sellprice, :isforrent, :furnish, :totalbedroom,
:unitstatus, :template_id, :floorplan_id, :unitplan_id,
unitrentperiods_attributes: [:rentprice, :rentperiod_id])
end
end
somehow it's not working. Did i miss something? need some guide to make it work.
At first you don't accept nested attributes, add
accept_nested_attributes_for :unitrentperiods
right below has_many :unitrentperiods
Also, you might need to configure strong parameters to allow that nested attributes parameter (or attr_accessible if you are on Rails 3).
Gotcha here: you have to allow unitrentperiods_attributes, not just unitrentperiods.

Rails hit new method on save of simpleform

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

Rails - getting the value out from the view form into a controller

In my form the user has to select a category for his post.
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
(Currently I am using text-input instead of a drop-down list)
Posts belong to categories
#post = #category.posts.build(post_params)
However I can't understand how to get the category value out of that field.
I have tried passing a number, to find_by id, and string to find_by name.
#category = Category.find(params[:category]) #returns no Categoy with nil id
#category = Category.find_by(name: params[:category]) #returns no method error
any help would be appreciated
Edit:
Form code
<%= form_for [#company, #post], :html => { :class => "form-posts"} do |f| %>
<div class="field form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon">$</span>
<%= f.text_field :text, class: "form-control", placeholder: "text", required: true %>
</div>
</div>
<div class="field form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<%= f.text_field :date,
class: "form-control",
value: #today,
data: {behaviour: "datepicker"},
required: true %>
</div>
</div>
<div class="field form-group">
<%= f.text_field :comment, class: "form-control", placeholder: "Comment (optional)" %>
</div>
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
<div class="actions"><%= f.submit 'Add', class: "btn btn-lg btn-primary" %></div>
<% end %>
Edit 2
controller:
form:
<%= f.text_field :category, class: "form-control" %>
def create
#company = current_user.companies.find(params[:company_id])
#category = #company.categories(params[:category])
#post = #category.posts.build(post_params)
debugger:
{"utf8"=>"✓", "authenticity_token"=>"...", "transaction"=>{"text"=>"lalala", "date"=>"11.11.2013", "comment"=>"", "category"=>"5"}, "commit"=>"Add post", "action"=>"create", "controller"=>"posts", "company_id"=>"2"}
undefined method `transactions'
Take a look at the development log.
Probably it will be something like params[:post][:category]
Get your category like this:
params[:post][:category]

Resources