Rails:尝试显示文件时有两个不同的NoMethodError

时间:2015-02-09 01:39:16

标签: ruby-on-rails ruby ruby-on-rails-4

我正在制作一个基本的应用程序,因此用户可以将文件附加到表单帖子。这一切都很好,但现在我正在尝试显示文件的链接,它似乎不起作用。

我收到两个错误。一个如果我附加一个文件而另一个如果我没有附加文件。他们都说undefined method 'doc=' for nil:NilClass但是在不同的代码行上。

如果我没有上传文件,这就是我在这行代码NoMethodError in Projects#index上获得的<% if @project.doc %>

如果我确实上传了一个文件,我就会得到NoMethodError in ProjectsController#create这段代码@project.doc = uploaded_io.original_filename

projects_controller.rb

class ProjectsController < ApplicationController

  def index
    @projects = Project.all
  end

  def show


  end

  def new
    @projects = Project.new

  end

  def create #no view
    @projects = Project.new(project_params)
    uploaded_io = params[:doc]
    if uploaded_io.present?
      File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
          file.write(uploaded_io.read)
          @project.doc = uploaded_io.original_filename
      end
    end
    if @projects.save
      redirect_to projects_path, :notice => "Your project was sent!"
    else 
      render "new"
    end
  end

  def edit
    @projects = Project.find(params[:id])
  end

  def update #no view
    @projects = Project.find(params[:id])

    if @projects.update_attributes(project_params)
      redirect_to projects_path, :notice => "Your project has been updated."
    else
      render "edit"
    end
  end

  def destroy #no view
    @projects = Project.find(params[:id])
    @projects.destroy
    redirect_to projects_path, :notice => "Your project has been deleted."
  end

  private
  def project_params
    params.require(:project).permit(:title, :description)
  end


end

index.html.erb

<div class="container">
  <div class="page-header">
    <h1>Projects<small> Here are all of your projects.</small></h1>
  </div>
</div>

<% @projects.each do |project| %>
<div class="container">
  <div class="panel panel-default">
  <div class="panel-heading">
  <%= project.title %>
  </div>
  </div>
  <div class="panel-body">
  <p>
  <%= project.description %> 
  </p>
  <br>
  <%= link_to "Discuss", new_project_discussion_path(project) %> |
  <%= link_to "Tasks", new_project_task_path(project) %> |
  <%= link_to "Edit", edit_project_path(project) %> |
  <%= link_to "Delete", project, :method => :delete %> 

  <% if @project.doc %>
    <p>Document: <%= link_to @project.doc, "/uploads/#{@project.doc}", :target => "_blank" %></p>
  <% end %>


  </div>
  </div>
<%end%>

<br>
<br>
<div class="container">
  <p><a class="btn btn-primary btn-lg" href="/projects/new" role="button">Create project</a></p>
</div>

_form.html.erb

<%= form_for(@projects, :html => { :multipart => true}) do |f| %>

  <% if @projects.errors.any? %>
    <ul>
      <% @projects.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  <% end %>



  <div class="container">
    <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: "form-control" %>
    </div>

    <div class="form-group">
    <%= f.label :description %>
    <%= f.text_area :description, class: "form-control" %>
    </div>

    <%= label_tag :doc, 'Files (optional)' %>
    <%= file_field_tag :doc %>

    <br>

    <div class="form-group">
    <%= f.submit "Submit Project", class: "btn btn-primary" %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

更新: 你有很多错误,这里有一些我发现的错误:

uploaded_io = params[:doc]

应该是

uploaded_io = params[:project][:doc]

同时删除此行

@project.doc = uploaded_io.original_filename

你不需要那个。

最后,在您的观看中,您应该project.doc而不是@project.doc