2017-10-26 9 views
0

リンクによって作成されているオブジェクトに属性を渡そうとしています。私は別のオブジェクトのショービューを見ています。:属性をfalseにし、もう一方を:属性をtrueにする2つのリンクを使用できるようにします。Rails 5.1リンクを使用してフォームに属性を渡そうとしています

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

コントローラ

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 
+0

あなたは何を保存しようとしていますか?共有してください。 –

+0

ブール値属性にtrueまたはfalseを渡そうとしています。リンクが新しいビューに開き、リンクを介して記入することなく属性のtrue/false値を保存したいと考えています。 –

+0

大丈夫ですが、共有したリンクは問題ありません。あなたの節約しようとしているかもしれません。 –

答えて

1

から:私はそれはそう、この属性のデフォルト値がfalseであると私は以下のようなものを使用してみましたが、それはただのデータベースではnilとして保存します設定していますリンクから新しいリンクを開いたときに、リースされた属性の自動設定が行われていると私は理解しています。

リンクにパラメータを渡す必要があります。コントローラで

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

そして、あなたは、チェックボックス(または他の制御)が選択表示されます新しい形で、その後

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

のようないくつかのことを行うことができます。

+0

ありがとうございました。出来た。もし私ができるなら、私はあなたにビールを買うよ –

+0

それはうまくいった –

関連する問題