form_tag
值發送到我的控制器
- 它看起來像PARAMS不見了,我得到這個錯誤:ActionController::ParameterMissing: param is missing or the value is empty: story
參數是丟失或爲空值hidden_field_tag
<% @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
任何想法是怎麼回事?
它期待':故事'PARAM,但你沒有提交。添加:'故事:<%= post [:story]%>'到列表中。看看是否解決了你的問題。 –
在您的控制器中,添加諸如'puts params.inspect'之類的東西,並查看您回來的內容(它將顯示在rails控制檯窗口中)。然後你可以看到你得到的實際參數與你期望看到的不同。通常這些都是由於一個額外的層。 –
注意:你的字符串已經足夠了......你不需要把它們放入另一個字符串中,使它們更加粘性,例如:''#{post [:username]}''可以'發佈[:用戶名]' –