param丢失或值为空hidden_​​field_tag

时间:2015-11-03 03:34:12

标签: ruby-on-rails ruby erb

我在通过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 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"}

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

知道出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您已通过storystrong_params方法中指定了参数中的story_params

视图端未传递嵌套在story param下的参数。这就是您收到此错误的原因。

要解决此问题,您可以将story_params方法更改为此(不包含require(:story)部分,因为您未在控制器中使用它):

def story_params
   params.permit(:description, :location, :avatar, :tag_list)
end