将资源(对象?)添加到 rails 应用程序后,如何让控制器保存它



我刚刚将其添加到我的 _form.html.erb 文件中

<div class="field">
    <%= f.label :street %><br />
    <%= f.text_field :street, autofocus: true, class: "form-control" %>
</div>

在我的 show.html.erb 文件中,我添加了这个

<div class="panel-body">
      <%= @property.description %>
      <%= @property.street %>
 </div>

但街道没有拯救。 我想我需要更改我的 properties_controller.rb 文件,但我不确定如何更改。

这是该文件:

class PropertiesController < ApplicationController
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:update, :edit, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @properties = Property.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 3)
  end
  def show
  end
  def new
    @property = current_user.property.build
  end
  def edit
  end
  def create
    @property = current_user.property.build(property_params)
      if @property.save
        redirect_to @property, notice: 'Property was successfully created.'
      else
        render :new
      end
    end

  def update
     if @property.update(property_params)
        redirect_to @property, notice: 'Property was successfully updated.'
     else
        render :edit
    end
  end
  def destroy
    @property.destroy
    respond_to do |format|
      format.html { redirect_to properties_url, notice: 'Property was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
  private
    def set_property
      @property = Property.find(params[:id])
    end
    def correct_user
        @property = current_user.property.find_by(id: params[:id])
        redirect_to property_path, notice: "Not authorized to edit this property" if @property.nil?
    end
    def property_params
      params.require(:property).permit(:description, :image)
    end
end

另一个相关的说明中,在 rails 中做地址表单的正确方法是什么? 我最终应该有这样的东西,

<div class="field">
    <%= f.label :street %><br />
    <%= f.text_field :street, autofocus: true, class: "form-control" %>
    <%= f.integer_field :zip, autofocus: true, class: "form-control" %>
    <%= f.text_field :city, autofocus: true, class: "form-control" %>
    <%= f.text_field :state, autofocus: true, class: "form-control" %>
</div>

感谢您的帮助:)

您需要

允许"街道"

def property_params
  params.require(:property).permit(:description, :image, :street)
end

最新更新