Ruby on rails 将资源(对象?)添加到rails应用程序后,如何让控制器保存它?
我刚刚把它添加到我的_form.html.erb文件中Ruby on rails 将资源(对象?)添加到rails应用程序后,如何让控制器保存它?,ruby-on-rails,Ruby On 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">
<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\u 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
类属性控制器params[:page],:per_page=>3)
结束
def秀
结束
def新
@property=当前_user.property.build
结束
定义编辑
结束
def创建
@property=当前用户.property.build(属性参数)
如果@property.save
将_重定向到@property,注意:“属性已成功创建。”
其他的
渲染:新
结束
结束
def更新
如果@property.update(属性参数)
将_重定向到@property,注意:“属性已成功更新。”
其他的
渲染:编辑
结束
结束
def销毁
@毁灭
回应待办事项|格式|
format.html{重定向到属性url,注意:'属性已成功销毁。}
format.json{head:no_content}
结束
结束
私有的
def set_属性
@property=property.find(参数[:id])
结束
def正确的用户
@property=当前用户.property.find\u by(id:params[:id])
重定向到属性路径,注意:如果@property.nil,则“无权编辑此属性”?
结束
def属性参数
参数require(:property).permit(:description,:image)
结束
结束
另一个相关的注意事项是,在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>
谢谢您的帮助:)您需要允许“street”
def property_params
params.require(:property).permit(:description, :image, :street)
end
检查是否出现批量分配错误,然后只需添加
params.require(:property).permit(:description,:image,:street)
谢谢。就这样。