开发者社区> 问答> 正文

我的提交按钮无法在Rails中的form_for上运行 - 没有错误消息

当我点击它时,我的form_for上的提交按钮没有做任何事情。我在控制台中确实有一条错误消息,上面写着“未经许可的参数:: photo_cache”但是当我看到这个时,我在控制器中的params中允许“photo_cache”,但是我的表单上的提交按钮仍然不起作用。

背景:我正在尝试创建一个具有以下参数的hairdressers:名称,描述,位置,照片和地址。

我的表格:

<%= simple_form_for(@hairdresser) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

<%= f.input :name %>

<%= f.input :address %>

<%= f.input :location %>

<%= f.input :description %>


<%= f.input :photo %>
<%= f.input :photo_cache, as: :hidden %>

<%= f.button :submit, label: "Submit Form", class: "btn btn-primary"  %>



<% end %>
我的控制器:

class HairdressersController < ApplicationController
def index

@hairdressers = Hairdresser.all

end

def show

@hairdresser = Hairdresser.find(params[:id])

end

def new

@hairdresser = Hairdresser.new

end

def create

@hairdresser = Hairdresser.new(hairdresser_params)
# @hairdresser.save ? (redirect_to hairdresser_path(@hairdresser)) : (render 'new')
  if @hairdresser.save
    redirect_to hairdresser_path(@hairdresser)
  else
    render 'new'
  end

end

def edit

@hairdresser = Hairdresser.find(params[:id])

end

def update

@hairdresser =  Hairdresser.find(params[:id])

end

def destroy

@hairdresser =  Hairdresser.find(params[:id])

end
end
private

def hairdresser_params
params.require(:hairdresser).permit(:name, :address, :photo, :location, :description, :photo_cache)
end

class Hairdresser < ApplicationRecord
belongs_to :user
validates :name, presence: true
validates :location, presence: true
validates :description, presence: true
validates :location, presence: true

mount_uploader :photo, PhotoUploader
end

展开
收起
小六码奴 2019-04-10 10:58:20 1817 0
1 条回答
写回答
取消 提交回答
  • “如果您使用的是Rails 5,则需要将用户关联更新为:

    belongs_to:user,optional:true“

    2019-07-17 23:33:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载