so, I used CarrierWave gem and followed a railscast demo to get it working, but the image doesn't show on my listing show page.
here's the code on the show page:
<p id="notice"><%= notice %></p>
<p>
<strong>Description:</strong>
<%= #listing.description %>
</p>
<p>
<strong>Price $:</strong>
<%= #listing.price %>
</p>
<p>
<strong>Subcategory:</strong>
<%= Subcategory.find(#listing.subcategory_id).subcategory_name %>
</p>
<p>
<strong>Choose an image to upload:</strong>
<%= image_tag #listing.image_url(:half).to_s %>
</p>
<%= link_to 'Edit', edit_listing_path(#listing) %> |
<%= link_to 'Back', listings_path %> |
<%= link_to 'Home', root_path %>
and here's the controller:
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_action :set_options, only: [:new, :edit, :update, :create]
# GET /listings
# GET /listings.json
def search
if params[:category_id].present?
#category = Category.find(params[:category_id])
#subcategory = nil
elsif params[:subcategory_id].present?
#subcategory = Subcategory.find(params[:subcategory_id])
#category = Category.find(#subcategory.category_id)
end
end
def index
#listings = Listing.all
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
#listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(listing_params)
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render :show, status: :created, location: #listing }
else
format.html { render :new }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
respond_to do |format|
if #listing.update(listing_params)
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: #listing }
else
format.html { render :edit }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.find(params[:id])
end
def set_options
#options_for_select_ary = Subcategory.all.collect {|subcategory| [ subcategory.subcategory_name, subcategory.id ] }
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:id, :description, :price, :subcategory_id)
end
end
and finally the model:
class Listing < ApplicationRecord
attr_accessor :image, :remote_image_url
belongs_to :category, optional: true
belongs_to :subcategory, optional: true
belongs_to :delivery, optional: true
belongs_to :user, optional: true
mount_uploader :image, AvatarUploader
end
so, my question is, how do I get the image to show up in the listing once it's been created?
I can add in my AvatarUploader class in an edit if you need to see the carrierwave code....
Related
sorry for this question but I'm struggling with this issue for hours now and can't find the answer anywhere.
Here is the thing, I have a rails app with "Reservation" and "Space" models with the following relations:
class Reservation < ActiveRecord::Base
belongs_to :space
belongs_to :user
end
class Space < ActiveRecord::Base
belongs_to :condo
has_many :reservations
end
When the user creates a new Reservation, in the form he gets to choose from a dropdown (f.select) the spaces available for him. The f.select in the form look like this:
<div class="field">
<%= #user_spaces = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user_spaces, :id, :name), :prompt => "Select space"
%>
</div>
That select it supose to assign a value to the key "space_id" in the Reservation that is being created (column's table is created). But when I check the last reservation in Rails console, space_id value is "nil". What am I doing wrong?
Thank you very much for your help
Reservation controller file:
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
#reservations = Reservation.all
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
#reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations
# POST /reservations.json
def create
#reservation = Reservation.new(reservation_params)
#user = current_user.id
#reservation.user_id = #user
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :new }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if #reservation.update(reservation_params)
format.html { redirect_to #reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: #reservation }
else
format.html { render :edit }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
#reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
#reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:eventdate)
end
end
Space controller file:
class SpacesController < ApplicationController
before_action :set_space, only: [:show, :edit, :update, :destroy]
# GET /spaces
# GET /spaces.json
def index
#spaces = Space.all
end
# GET /spaces/1
# GET /spaces/1.json
def show
end
# GET /spaces/new
def new
#space = Space.new
end
# GET /spaces/1/edit
def edit
end
# POST /spaces
# POST /spaces.json
def create
#space = Space.new(space_params)
respond_to do |format|
if #space.save
format.html { redirect_to #space, notice: 'Space was successfully created.' }
format.json { render :show, status: :created, location: #space }
else
format.html { render :new }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /spaces/1
# PATCH/PUT /spaces/1.json
def update
respond_to do |format|
if #space.update(space_params)
format.html { redirect_to #space, notice: 'Space was successfully updated.' }
format.json { render :show, status: :ok, location: #space }
else
format.html { render :edit }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# DELETE /spaces/1
# DELETE /spaces/1.json
def destroy
#space.destroy
respond_to do |format|
format.html { redirect_to spaces_url, notice: 'Space was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_space
#space = Space.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def space_params
params.require(:space).permit(:name)
end
end
And full Reservation Form:
<%= form_for(#reservation) do |f| %>
<% if #reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% #reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :eventdate %><br>
<%= f.date_select :eventdate %>
</div>
<div class="field">
<%= #user = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user, :id, :name), :prompt => "Select space"
%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
pretty sure you need to permit the space_id attribute in your strong params.
def reservation_params
params.require(:reservation).permit(:eventdate, :space_id)
end
whats happening is that when you go to create a reservation, youre passing in set of params, that is the output of reservation_params
#reservation = Reservation.new(reservation_params)
if space_id is not being permitted in your strong params, then it will be nil when created.
if this is not the issue, can you post what params are getting to the server, and what the output of reservation_params are.
I have a model "post" and a model "photo". The model post have a nested form from photo to upload images with association. The upload works fine, but everytime I go to edit the post, I have a new input to each image uploaded and one more input to upload a new image. I want to have just the input to upload a new file and hide or not load the others.
Here is my code:
<%= f.fields_for :photos do |photo| %>
<%= photo.file_field :image, class: "form-control" %>
<% end %>
Controller:
module Admin
class SeminovosController < SuperAdminController
before_action :set_seminovo, only: [:show, :edit, :update, :destroy]
# GET /seminovos
# GET /seminovos.json
def index
#seminovos = Seminovo.all
#seminovo = Seminovo.new
#seminovo.photos.build
#expires_in 3.hours, :public => true, 'max-stale' => 0
end
# GET /seminovos/1
# GET /seminovos/1.json
def show
end
# GET /seminovos/new
def new
#seminovo = Seminovo.new
#seminovo.photos.build
#seminovo.photos
end
# GET /seminovos/1/edit
def edit
#seminovo.photos.build
#seminovo.photos
end
# POST /seminovos
# POST /seminovos.json
def create
##seminovo = Seminovo.new(seminovo_params)
respond_to do |format|
#seminovo = Seminovo.new(seminovo_params)
#seminovo.save
format.html { redirect_to #seminovo }
format.js
# if #seminovo.save
# format.html { redirect_to #seminovo, notice: 'Seminovo was successfully created.' }
# format.json { render :show, status: :created, location: #seminovo }
# else
# format.html { render :new }
# format.json { render json: #seminovo.errors, status: :unprocessable_entity }
# end
end
end
# PATCH/PUT /seminovos/1
# PATCH/PUT /seminovos/1.json
def update
respond_to do |format|
if #seminovo.update(seminovo_params)
format.html { redirect_to #seminovo, notice: 'Seminovo was successfully updated.' }
format.json { render :show, status: :ok, location: #seminovo }
else
format.html { render :edit }
format.json { render json: #seminovo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /seminovos/1
# DELETE /seminovos/1.json
def destroy
#seminovo.destroy
respond_to do |format|
format.html { redirect_to seminovos_url, notice: 'Seminovo was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_seminovo
#seminovo = Seminovo.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def seminovo_params
params.require(:seminovo).permit(:name, :price, :marca_id, :tipo_id, :ano_modelo,
:portas, :km_rodados, :combustivel, :placa, :cor, :desc,
:video, :destaque, :photo_id, :slug,
photos_attributes: [ :id, :image, :image_uid, :image_name, :desc,
:seminovos_id, :_destroy ])
end
end
end
Remove
#seminovo.photos.build
#seminovo.photos
from new and edit action.
Then replace
<%= f.fields_for :photos do |photo| %>
<%= photo.file_field :image, class: "form-control" %>
<% end %>
With
<%= f.fields_for :photos, #seminovo.photos.new do |photo| %>
<%= photo.file_field :image, class: "form-control" %>
<% end %>
I'm having a hard time implementing a checkout process where once at the cart, user can checkout and have order shipped. Maybe I need an order model and transaction controller?
I'm just not sure how to set those up. Currently the cart works and can be cleared as well as have items be added, its just I'm not sure how to implement a checkout and order system.
Idea is: User at cart clicks the checkout button, then is taken to checkout where he/she can input payment information, then taken back to products page. Issue is I'm not sure again how to connect the cart to the checkout and payment process into one simple easy system.
Any help would be appreciated, I'm still very new at this. Thank you.
class CartController < ApplicationController
before_action :authenticate_user!, except: [:index]
def add
id = params[:id]
if session[:cart] then
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
if cart[id] then
cart[id] = cart[id] + 1
else
cart[id] = 1
end
redirect_to :action => :index
flash[:notice] = 'added to cart'
end
def clearCart
session[:cart] = nil
redirect_to :action => :index
flash[:notice] = 'cart cleared'
end
def index
if session[:cart] then
#cart = session[:cart]
else
#cart = {}
end
end
end
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
#products = Product.all
end
def show
end
def new
#product = Product.new
end
def edit
end
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_product
#product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :price, :category, :subcategory)
end
end
class Product < ActiveRecord::Base
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
views/cart/index.html.erb
<h1>Your Cart</h1>
<% if #cart.empty? %>
<p> Your cart is currently empty</p>
<% else %>
<%= link_to 'Empty your Cart', cart_clear_path %>
<br><br>
<% end %>
<% total = 0 %>
<ul>
<% #cart.each do | id, quantity | %>
<% product = Product.find_by_id(id) %>
<li>
<%= link_to product.title, product %>
<p><%= product.description %></p>
<p><%= number_to_currency product.price %></p>
<p>Quantity: <%= quantity %></p>
</li>
<% total += quantity * product.price %>
<% end %>
<p><b><%= number_to_currency total, :unit => '$' %> </b></p>
</ul>
What I've tried below
order.rb
class Order < ActiveRecord::Base
belongs_to :cart
end
cart.rb
class Cart < ActiveRecord::Base
has_many :line_items
has_one :order
end
Orders_Controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
#orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
#order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:new, :cart_id, :ip_address, :first_name, :last_name, :user_id)
end
end
added Checkout to views/cart/index.html.erb
<%= link_to "Checkout", new_order_path, class: "btn btn-primary" %>
What do I do after this?
you've used a local variable product, not the controller attribute #product. (just add the '#' sign)
my advise to you is to use a gem like shoppe,
you can see example here (demo)
you can also navigate through the models of the gem to see how they did it, maybe it will give you a clear perspective of writing an e-commerce solution.
I am having 2 models:
app/models/employee.rb:
class Employee < User
has_many :insurances
end
app/models/insurance.rb:
class Insurance < ActiveRecord::Base
belongs_to :employee
end
app/controllers/employees_controller.rb:
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
before_action :employee_params, only: [:create, :update]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
#employee = Employee.find(params[:id])
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
#employee = Employee.find(params[:id])
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to employees_url, notice: "#{#employee.first_name} was successfully created." }
format.json { render :index, status: :created, location: #employee }
else
format.html { render :new }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to employees_url, notice: "#{#employee.first_name} was successfully updated."}
format.json { render :index, status: :ok, location: #employee }
else
format.html { render :edit }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
if params[:employee][:password].blank? && params[:employee][:password_confirmation].blank?
params[:employee].delete(:password)
params[:employee].delete(:password_confirmation)
end
params[:employee].permit(:email, :password, :employee_id,:employee_work_id, :first_name, :middle_name, :last_name, :gender, :date_of_birth, :driver_license_no, :driver_license_expiry_date, :martial_status, :nationality, :office_address, :residence_address, :city, :state_province, :zip_code, :country, :work_number, :mobile_number, :home_number, :other_email)
end
end
app/controllers/insurance_controller.rb:
class InsurancesController < ApplicationController
before_action :set_insurance, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#insurances = Insurance.all
respond_with(#insurances)
end
def show
respond_with(#insurance)
end
def new
#insurance = Insurance.new
respond_with(#insurance)
end
def edit
end
def create
#insurance = Insurance.new(insurance_params)
#insurance.save
respond_with(#insurance)
end
def update
#insurance.update(insurance_params)
respond_with(#insurance)
end
def destroy
#insurance.destroy
respond_with(#insurance)
end
private
def set_insurance
#insurance = Insurance.find(params[:id])
end
def insurance_params
params.require(:insurance).permit(:employee_id,:name_of_dependent, :relationship, :name, :of_spouse, :children, :date_of_birth, :policy_number, :policy_provider, :policy_type)
end
end
app/views/insurances/_show.html.erb:
<%= employee.insurances.each do |emp| %>
<p>
<strong>Name of dependent:</strong>
<%= emp.name_of_dependent %>
</p>
<p>
<strong>Name:</strong>
<%= emp.name %>
</p>
<% end %>
When i use link_to with show path, it is working fine.
app/views/employees/show.html.haml:
%p
%strong Title:
= #employee.full_name
%p
%strong Text:
= #employee.gender
%p
= link_to 'Insurance', insurance_path
after accordion code, i used render as follows:
%p
%strong Title:
= #employee.full_name
%p
%strong Text:
= #employee.gender
%p
#accordion2.panel-group
#new-student-widget.panel.panel-default.left-column-entry
.header.panel-heading
.header-content.panel-title
%a#newStudentToggle{"data-parent" => "#accordion2", "data-target" => "#newStudent", "data-toggle" => "collapse"} Insurance Details
#newStudent.panel-collapse.collapse
#newStudentInner.panel-body
= render :template => 'insurances/show', locals: { employee: #employee }
When i am using render in accordion, i am getting error as:
NoMethodError in Employees#show
undefined method `name_of_dependent' for nil:NilClass
Please help me out.
If you are calling show as partial then rename show.html to _show.html
and pass #employeee instance variable which is present in accordion code
= render :template => 'insurances/show', locals: { employee: #employee }
and in _show page
- employee.insurances.each do |ins|
%p.strong Name of dependent:
= ins.name_of_dependent
%p.strong Name:
= ins.name
Pass the insurance object when you are rendering the template :
- #insurances.each do |insurancce|
= render template: 'insurances/show', locals: { insurance: insurance }
- end
Make sure you instantiated the #insurance object in your controller action, where you instantiated the #employee object.
#employee = current_user # or anything else
#insurances = #employee.insurances
Then in your Insurances#show template use insurance instead of #insurance like this :
<p id="notice"><%= notice %></p>
<p>
<strong>Name of dependent:</strong>
<%= insurance.name_of_dependent %>
</p>
<p>
<strong>Name:</strong>
<%= insurance.name %>
</p>
I'm fairly new to rails, building my first app. I'm running rails 4 w/ bootstrap 3. I'm trying to get a complex form to work. I have two models:
class Employee < ActiveRecord::Base
belongs_to :company
belongs_to :user, :through => :company
has_one :position
accepts_nested_attributes_for :position
end
class Position < ActiveRecord::Base
belongs_to :employees
accepts_nested_attributes_for :employees
end
I have a form where the User can create a new job title (Position Model) and select the employees (Employees Model) that position will be applied to. Basically it's a single form that will add fields to 2 different database tables (Position and Employee).
This is my view:
<%= simple_form_for(#position) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :job_title %>
<%= f.input :job_description %>
</div>
<%= f.fields_for :Employee do |f| %>
<%= f.input :employee_title, label: "Apply to:", collection: Employee.all, label_method: :first_name, as: :check_boxes %>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Below are the controllers:
class PositionsController < ApplicationController
before_action :set_position, only: [:show, :edit, :update, :destroy]
# GET /positions
# GET /positions.json
def index
#positions = Position.all
end
# GET /positions/1
# GET /positions/1.json
def show
end
# GET /positions/new
def new
#position = Position.new
end
# GET /positions/1/edit
def edit
end
# POST /positions
# POST /positions.json
def create
#position = Position.new(position_params)
respond_to do |format|
if #position.save
format.html { redirect_to #position, notice: 'position was successfully created.' }
format.json { render action: 'show', status: :created, location: #position }
else
format.html { render action: 'new' }
format.json { render json: #position.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /positions/1
# PATCH/PUT /positions/1.json
def update
respond_to do |format|
if #position.update(position_params)
format.html { redirect_to #position, notice: 'position was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #position.errors, status: :unprocessable_entity }
end
end
end
# DELETE /positions/1
# DELETE /positions/1.json
def destroy
#position.destroy
respond_to do |format|
format.html { redirect_to positions_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_position
#position = Position.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def position_params
params.require(:position).permit(:position_title, :position_description, :position_create_date)
end
end
class EmployeesController < ApplicationController
# encoding: UTF-8
before_action :set_employee, only: [:show, :edit, :update, :destroy]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to #employee, notice: 'Employee was successfully created.' }
format.json { render action: 'show', status: :created, location: #employee }
else
format.html { render action: 'new' }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to #employee, notice: 'Employee was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:first_name, :last_name, :employee_title)
end
end
The problem I'm facing is I get the form to render perfectly, but when I submit it, only fields that belong to the Position model get recorded. The :employee_title stays blank. Any suggestions what the problem is?
Thank you!!
Too many points to fit in a comment:
belongs_to :employees should be singular: belongs_to :employee
Your fields_for should be like (differentiate the ff from parent form):
<%= f.fields_for :Employee do |ff| %>
<%= ff.input :employee_title, label: "Apply to:", collection: Employee.all,
label_method: :first_name, as: :check_boxes %>
<% end %>
If it doesn't work, supply your controller also and I'll update my answer.
Edit:
After seeing your controllers, it seems most likely to be case of unpermitted params.
In position_controller.rb add employee params to position params
def position_params
params.require(:position).permit(:position_title, :position_description, :position_create_date, employees_attributes: [:first_name, :last_name, :employee_title])
end