缺少参数或值为空 hidden_field_tag
param is missing or the value is empty hidden_field_tag
我在通过 form_tag
向我的控制器发送值时遇到问题 -- 似乎缺少参数,我收到此错误:ActionController::ParameterMissing: param is missing or the value is empty: story
<% @locations.each do |post| %>
<div class="box col3">
<img src="<%= post[:image] %>">
<small>username: </small><%= post[:username]%>
<small>posted date: </small><%= post[:created_time] %>
<small>place: </small><%= post[:place] %>
<small>tags: </small> <%= post[:hash_tags] %>
<small>lat: </small> <%= post[:lat] %>
<small>lg: </small> <%= post[:lg] %>
<div class="like-button">
<%= form_tag('/stories/create', remote: true) do %>
<%= hidden_field_tag 'avatar', "#{post[:image]}" %>
<%= hidden_field_tag 'description', "#{post[:username]}" %>
<%= submit_tag "Like", class: "btn btn-warning like-button" %>
<% end %>
</div>
</div>
<%end%>
@locations
是一个哈希数组。
例如 @locations.first
产量:
{:lat=>40.7519798,
:lg=>-73.9475174,
:place=>"Z Hotel NY",
:profile_picture=>
"http://photos-g.ak.instagram.com/hphotos-ak-xtp1/t51.2885-19/s150x150/12105211_896812917070398_1478405438_a.jpg",
:hash_tags=>[],
:username=>"dannessex90",
:fullname=>"Dann Essex",
:created_time=>"2015-11-02 22:41:25 -0500",
:image=>
"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11421986_972505559476106_241708523_n.jpg"}
story controller
:
class StoriesController < ApplicationController
def index
@stories = Story.all
end
def show
end
def new
@story = Story.new
end
def edit
end
def create
current_location = request.remote_ip
binding.pry
coordinates = Location.get_coord(story_params[:location])
params = {description: story_params[:description], location: story_params[:location], latitude: coordinates[0], longitude: coordinates[1], avatar: story_params[:avatar]}
@story = current_user.stories.build(params)
if @story.save
redirect_to url_for(:controller => :users, :action => :my_stories)
else
render 'searches/result'
end
end
private
def story_params
params.require(:story).permit(:description, :location, :avatar, :tag_list)
end
end
知道出了什么问题吗?
发生这种情况是因为您已指定通过 strong_params
在您的参数中要求 story
,即在您的 story_params
方法中。
视图侧未传递嵌套在 story
参数下的参数。这就是您收到此错误的原因。
要解决此问题,您可以将 story_params
方法更改为此(没有 require(:story)
部分,因为您未在控制器中使用它):
def story_params
params.permit(:description, :location, :avatar, :tag_list)
end
我在通过 form_tag
向我的控制器发送值时遇到问题 -- 似乎缺少参数,我收到此错误:ActionController::ParameterMissing: param is missing or the value is empty: story
<% @locations.each do |post| %>
<div class="box col3">
<img src="<%= post[:image] %>">
<small>username: </small><%= post[:username]%>
<small>posted date: </small><%= post[:created_time] %>
<small>place: </small><%= post[:place] %>
<small>tags: </small> <%= post[:hash_tags] %>
<small>lat: </small> <%= post[:lat] %>
<small>lg: </small> <%= post[:lg] %>
<div class="like-button">
<%= form_tag('/stories/create', remote: true) do %>
<%= hidden_field_tag 'avatar', "#{post[:image]}" %>
<%= hidden_field_tag 'description', "#{post[:username]}" %>
<%= submit_tag "Like", class: "btn btn-warning like-button" %>
<% end %>
</div>
</div>
<%end%>
@locations
是一个哈希数组。
例如 @locations.first
产量:
{:lat=>40.7519798,
:lg=>-73.9475174,
:place=>"Z Hotel NY",
:profile_picture=>
"http://photos-g.ak.instagram.com/hphotos-ak-xtp1/t51.2885-19/s150x150/12105211_896812917070398_1478405438_a.jpg",
:hash_tags=>[],
:username=>"dannessex90",
:fullname=>"Dann Essex",
:created_time=>"2015-11-02 22:41:25 -0500",
:image=>
"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11421986_972505559476106_241708523_n.jpg"}
story controller
:
class StoriesController < ApplicationController
def index
@stories = Story.all
end
def show
end
def new
@story = Story.new
end
def edit
end
def create
current_location = request.remote_ip
binding.pry
coordinates = Location.get_coord(story_params[:location])
params = {description: story_params[:description], location: story_params[:location], latitude: coordinates[0], longitude: coordinates[1], avatar: story_params[:avatar]}
@story = current_user.stories.build(params)
if @story.save
redirect_to url_for(:controller => :users, :action => :my_stories)
else
render 'searches/result'
end
end
private
def story_params
params.require(:story).permit(:description, :location, :avatar, :tag_list)
end
end
知道出了什么问题吗?
发生这种情况是因为您已指定通过 strong_params
在您的参数中要求 story
,即在您的 story_params
方法中。
视图侧未传递嵌套在 story
参数下的参数。这就是您收到此错误的原因。
要解决此问题,您可以将 story_params
方法更改为此(没有 require(:story)
部分,因为您未在控制器中使用它):
def story_params
params.permit(:description, :location, :avatar, :tag_list)
end