无法为我的Rails应用添加表单

时间:2018-08-08 19:41:39

标签: ruby-on-rails forms nested

我对论坛,rails和编程非常陌生。我只是在构建一个应用程序来组织我的钢琴调音。

我有一个调整模型tuning.rb

class Tuning < ApplicationRecord
  has_many :tuneds
  accepts_nested_attributes_for :tuneds
  validates :Client, presence: true
  validates :Tel, presence: true
  validates :Address, presence: true
  geocoded_by :Address
  after_validation :geocode

  def start_time 
    self.scheduled_date.start
  end
end

有很多调整。我的调整模型是tuned.rb

class Tuned < ApplicationRecord
  belongs_to :tuning
  accepts_nested_attributes_for :tuning
end

我想像这样在调谐显示视图上显示我的调谐形式 [显示表格] [1]

我已经将它嵌入了simple_form

<%= simple_form_for :tuneds do |r| %>
  <%= r.input :pitch %>
  <%= r.input :payment %>
  <%= r.input :work_done %>
  <%= r.button :submit %>
<% end %>                      

但是,它不会将记录添加到上面的“我的记录”列中,因为我可以通过控制台添加它们,所以我知道这是可行的。它不会出现在控制台或显示页面中 [未显示提交记录] [2]

感谢您的帮助!埃文

 [1]: https://i.stack.imgur.com/JZ2vR.png
  [2]: https://i.stack.imgur.com/87k7A.png

class TuningsController < ApplicationController
  before_action :set_tuning, only: [:show, :edit, :update, :destroy]

  # GET /tunings
  # GET /tunings.json 
  # how to get them to appear only upcoming Tuning.where('scheduled_date' => Date.today)
  def index

    @tunings = Tuning.all.order('scheduled_date DESC')
    add_breadcrumb "Home", :root_path
    add_breadcrumb "All Tunings"
    if params[:search].present?
@tunings = Tuning.all
@hash = Gmaps4rails.build_markers(@tunings) do |user, marker|
  marker.lat user.latitude
  marker.lng user.longitude
  @list =Tuning.find(params[:id])
@json = @list.to_gmaps4rails

@tuneds = Tuned.all? { |
|  }
end
    end

  end


Tuning_controller controller
     # GET /tunings/1
      # GET /tunings/1.json
      def show
          add_breadcrumb "Up-coming tunings", :root_path
      end

      def current
        @tuning_current = Tuning.where(['scheduled_date > ?', 1.day.ago]).order('scheduled_date ASC')
           add_breadcrumb "Home", :root_path
           add_breadcrumb "Up-coming Tunings", :tunings_current_path

    end



    def bookagain
      six_months = 6.months.ago
      @tuning_bookagain = Tuning.where(['scheduled_date < ?', 6.months.ago])

      add_breadcrumb "Home", :root_path
      add_breadcrumb "re-book", :tunings_bookagain_path


    end

    def reports
        add_breadcrumb "Home", :root_path
        add_breadcrumb "Reports", :tunings_reports_path
     end

      # GET /tunings/new
      def new
        @tuning = Tuning.new
        add_breadcrumb "Up-coming tunings", :root_path

      end

      # GET /tunings/1/edit
      def edit
      end

      # POST /tunings
      # POST /tunings.json
      def create
        @tuning = Tuning.new(tuning_params)

        respond_to do |format|
          if @tuning.save
            format.html { redirect_to @tuning, notice: 'Tuning was successfully added.' }
            format.json { render :show, status: :created, location: @tuning }
          else
            format.html { render :new }
            format.json { render json: @tuning.errors, status: :unprocessable_entity }
          end
        end
        add_breadcrumb "Up-coming tunings", :root_path


      end


      # PATCH/PUT /tunings/1
      # PATCH/PUT /tunings/1.json
      def update
        respond_to do |format|
          if @tuning.update(tuning_params)
            format.html { redirect_to @tuning, notice: 'Tuning was successfully updated.' }
            format.json { render :show, status: :ok, location: @tuning }
          else
            format.html { render :edit }
            format.json { render json: @tuning.errors, status: :unprocessable_entity }
          end

        end
          add_breadcrumb "home", :root_path
      end

      # DELETE /tunings/1
      # DELETE /tunings/1.json
      def destroy
        @tuning.destroy
        respond_to do |format|
          format.html { redirect_to tunings_url, notice: 'Tuning was successfully deleted.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_tuning
          @tuning = Tuning.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def tuning_params
          params.require(:tuning).permit(:Client, :scheduled_date, :Tel, :Address, :Quote, :Type, :Make, :Model, :Work_done, :Work_needed, :Comments, :Paid, :Initial_contact, :Sn)
        end





  Tuned_controller:

class TunedsController < ApplicationController
    def index
        @tuning = Tuning.find(params[:tuning_id])
        @tuneds = @tuning.tuneds.create(tuneds.params)
end
end

再次感谢

很可能会将id传递给tuneds模型?我得到的错误是 ‘没有路线与[POST]“ / tunings / 51”匹配

1 个答案:

答案 0 :(得分:-1)

我发现解决这个问题的最简单方法是重新制作脚手架,但是这次添加了参考:调整我的脚手架,这是第一次以来缺少的。

埃文