【问题标题】:Rails trying to fetch data using foreach but getting errorRails 尝试使用 foreach 获取数据但出现错误
【发布时间】:2016-05-12 09:22:33
【问题描述】:

我正在构建一个博客应用程序。我有一个帖子控制器我在视图标题、正文和文件中有三个字段。我正在将此文件属性传递给我的模型,我正在尝试使用 foreach 获取数据但无法这样做..Getting this error undefined method [] for ActionDispatch::Http::UploadedFile:0x000000035a1970


[post_Controller.rb]

class PostsController < ApplicationController
    before_action :authenticate_user! 

    def index
        @posts = Post.user_post(current_user).order('created_at DESC').paginate(:page => params[:page], :per_page => 5)

    end 

    def new
        @post = Post.new

    end

    def show
        @post = find_params
    end

    def create
        @post = Post.new(post_params)

        @post.user = current_user


        if @post.save
            Post.upload(params[:post][:files])

            redirect_to @post
        else
            render 'new'
        end
    end

    def edit
        @post = find_params
    end

    def update
        @post = find_params

        if @post.update(post_params)
            redirect_to @post
        else
            render 'edit'
        end
    end

    def destroy
        @post = find_params
        @post.destroy

        redirect_to posts_path
    end

    private
    def post_params
        params.require(:post).permit(:title, :body)
    end

    def find_params
        Post.find(params[:id])
    end


end
 <br>

[post.rb]

class Post < ActiveRecord::Base 

    has_many :comments, dependent: :destroy
    belongs_to :user

    validates :title, presence: true, length: {minimum: 5}
    validates :body,  presence: true


    def self.user_post(id)
        role = User.find_role(id)
        if role == 'admin'
            Post.all

        elsif role == 'user'
            Post.where(user_id: id)
        elsif role == 'developer'

        end             
    end
**#########at this point i am getting error#####**
    def self.upload(files)
            k=files[:post][:title]
    end

end


[posts/_form.html.erb]

<%= form_for @post,html: { multipart: true } do |f| %>
    <% if @post.errors.any? %>
        <div id="errors">
            <h2><%= pluralize(@post.errors.count, "error") %> prevented this post from saving:</h2>
            <ul>
                <% @post.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>
    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
    <br>
    <%= f.label :body %><br>
    <%= f.text_field :body %><br>

     <br>
    <%= f.label :files %><br>
    <%= f.file_field :files %><br>


    <%= f.submit %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您已经将params[:post][:files] 传递给upload 方法,因此您不再需要[:post][:files]。你可以直接使用files:

    def self.upload(files)
      files.each do |file|
        # Do something with file
      end
    end
    

    另外,&lt;ActionDispatch::Http::UploadedFile:0x00000003d40b68&gt; 表示您只有 1 个上传的文件,这不是一个集合,因此您不能在上面使用each。您必须更改表单以允许上传多个文件。

    试试这个:

    <%= f.file_field :files, :multiple => true %>
    

    【讨论】:

    • 我已经尝试过了,它给我错误 undefined method `each' for #<:http::uploadedfile:0x00000003d40b68>
    • 在这种情况下我需要知道如何使用 foreach 获取数据
    • 在 foreach 中写什么以便我可以获取数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2020-01-14
    • 2022-09-24
    相关资源
    最近更新 更多