我在通过 form_tag
向我的 Controller 发送值时遇到问题——看起来参数丢失了,我得到了这个错误:ActionController::ParameterMissing: param is missing or the值为空:故事
<% @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
yield:
{: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"}
故事 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
方法中。
View 端未传递嵌套在 story
参数下的参数。这就是您收到此错误的原因。
要解决此问题,您可以将 story_params
方法更改为此(没有 require(:story)
部分,因为您没有在 Controller 中使用它) :
def story_params
params.permit(:description, :location, :avatar, :tag_list)
end
关于ruby-on-rails - 参数缺失或值为空 hidden_field_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490949/