After adding a resource (object?) to rails app, how do I get the controller to save it? -
i added _form.html.erb file
<div class="field"> <%= f.label :street %><br /> <%= f.text_field :street, autofocus: true, class: "form-control" %> </div>
and in show.html.erb file take street input added this
<div class="panel-body"> <%= @property.description %> <%= @property.street %> </div>
but street not saving. think need change properties_controller.rb file, i'm not sure how.
here file:
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 created.' else render :new end end def update if @property.update(property_params) redirect_to @property, notice: 'property updated.' else render :edit end end def destroy @property.destroy respond_to |format| format.html { redirect_to properties_url, notice: 'property 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 edit property" if @property.nil? end def property_params params.require(:property).permit(:description, :image) end end
on related note, proper way address form in rails? should have like,
<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>
thanks :)
you need permit 'street'
def property_params params.require(:property).permit(:description, :image, :street) end
Comments
Post a Comment