Luis Mejia
Luis Mejia

Reputation: 87

Rails 5.1 Trying to pass an attribute to a form using a link

I am trying to pass an attribute to an object that is being created by a link. I am on the show view of another object and I want to have two links available one that will make the :attribute false and the other to make the :attribute true. I have it set up so the default value of the this attribute is false and I tried using something like below, but it just saves it as nil in the database:

<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(@building, @listing, @appointment, @rented_unit, leased: true) %>>

controller

class RentedUnitsController < ApplicationController
  before_action :building
  before_action :listing
  before_action :appointment
  before_action :set_rented_unit, only: [:show, :edit, :update, :destroy]

  # GET /rented_units
  # GET /rented_units.json
  def index
    @rented_units = appointment.rented_units
  end

  # GET /rented_units/1
  # GET /rented_units/1.json
  def show
  end

  # GET /rented_units/new
  def new
    @rented_unit = appointment.rented_units.new
  end

  # GET /rented_units/1/edit
  def edit
  end

  # POST /rented_units
  # POST /rented_units.json
  def create
    @rented_unit = appointment.rented_units.new(rented_unit_params)

    respond_to do |format|
      if @rented_unit.save
        format.html { redirect_to [building, listing, appointment, @rented_unit], notice: 'Rented unit was successfully created.' }
        format.json { render :show, status: :created, location: @rented_unit }
      else
        format.html { render :new }
        format.json { render json: @rented_unit.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /rented_units/1
  # PATCH/PUT /rented_units/1.json
  def update
    respond_to do |format|
      if @rented_unit.update(rented_unit_params)
        format.html { redirect_to [building, listing, appointment, @rented_unit], notice: 'Rented unit was successfully updated.' }
        format.json { render :show, status: :ok, location: @rented_unit }
      else
        format.html { render :edit }
        format.json { render json: @rented_unit.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /rented_units/1
  # DELETE /rented_units/1.json
  def destroy
    @rented_unit.destroy
    respond_to do |format|
      format.html { redirect_to building_listing_appointment_rented_units_path(@building, @listing, @appointment), notice: 'Rented unit was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_rented_unit
      @rented_unit = appointment.rented_units.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def rented_unit_params
      params.require(:rented_unit).permit(:unit_no, :unit_model, :price, :bedrooms, :bathrooms, :half_baths, :square_footage, :leased, :appointment_id)
    end

    def building
        @building ||= Building.find(params[:building_id])
    end

    def listing
        @listing ||= Listing.find(params[:listing_id])
    end

    def appointment
        @appointment ||= Appointment.find(params[:appointment_id])
    end
end

Upvotes: 0

Views: 46

Answers (1)

Ziyan Junaideen
Ziyan Junaideen

Reputation: 3300

From what I understand you are looking to populate leased attribute auto when you open a new from from the link.

You need to give the param param to the link.

<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(@building, @listing, @appointment, @rented_unit, rented_unit: { leased: true } ) %>>

In the controller then you can do some thing like

  # GET /rented_units/new
  def new
    @rented_unit = appointment.rented_units.new(rented_unit_params)
  end

Then, in the new form you will see the checkbox (or other control) selected.

Upvotes: 1

Related Questions