Rails:param丢失或值为空。控制器中的强参数

时间:2014-10-02 10:29:59

标签: ruby-on-rails controller strong-parameters

我有这个控制器

class StoresController < ApplicationController
  before_filter :authenticate_business!, :except => [:index, :show]

def index
  #@stores = Store.paginate(:page => params[:page])#, :per_page => 8)

  if params[:query].present?
    @stores = Store.search(params[:query], page: params[:page])
  else
    @stores = Store.all.page params[:page]
  end
end

def show
  @store = Store.friendly.find(params[:id])

  if request.path != store_path(@store)
    redirect_to @store, status: :moved_permanently
  end
end

def new
  @store = Store.new
end

def create
  @store = Store.new(store_params)

  @store.business_id = current_business.id

  if @store.save
    redirect_to @store
  else
    render 'new'
  end
end

def edit
  @store = Store.friendly.find(params[:id])
end

def update
  @store = Store.friendly.find(params[:id])

  if @store.update(store_params)
    redirect_to @store
  else
    render 'edit'
  end
end

def destroy
  @store = Store.friendly.find(params[:id])

  @store.destroy

  redirect_to stores_url
end


private
  def store_params
    params.require(:store).permit(:name, :description, :address, :telephone, :email, :website)
  end

end

以及带有表单的视图以创建新商店。

<%= form_for @store do |f| %>

.......
code
......

<% end %>

问题是,当我提交表单时,它会给我这个错误“param is missing or value is empty:store”,指向“params.require(:store).permit”行::name,:description ,:地址,:电话,:电子邮件,:网站)“

有什么想法解决这个问题吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这是由路径问题引起的,如评论中所述,导致表单不发布任何数据。

我认为您需要确保“获取”请求到“新”路由访问您的“新”方法,而“发布”请求到“新”路由访问您的“创建”方法。类似的东西:

/* entire container, keeps perspective */
.flip-container {
perspective: 1000;
}

/* flip the pane when hovered */
.flip-container:hover .flipper, .flip-container.hover .flipper {
    transform: rotateY(180deg);
}

.flip-container, .front, .back {
width: 320px;
height: 480px;
}

/* flip speed goes here */
.flipper {
transition: 0.6s;
transform-style: preserve-3d;

position: relative;
}

/* hide back of pane during swap */
.front, .back {
backface-visibility: hidden;

position: absolute;
top: 0;
left: 0;
}

/* front pane, placed above back */
.front {
z-index: 2;
/* for firefox 31 */
transform: rotateY(0deg);
}

/* back, initially hidden pane */
.back {
transform: rotateY(180deg);
}