0

My update.html.erb:

<% @uploads.each do |upload| %>

        <p><%= upload.name %></p>               
        <p class="grey">

    <%= best_in_place upload, :place, type: :select, collection: [ ["Home", "Home"],["Sauna", "Sauna"]]%>  

            </p>    
  <%end%>

controller:

 def show
      @uploads = Upload.all 
  end
  def update
    @uploads = Upload.all
    @upload = Upload.find(params[:id])

    respond_to do |format|
      if @upload.update_attributes(params[:upload])
        format.html { redirect_to @upload, notice: 'Upload was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @upload.errors, status: :unprocessable_entity }
      end
    end
  end

The problem is that I get the error: undefined methodeach' for nil:NilClass` , meaning that my controllers do not pass my object to the variable @uploads. Why do not the do that and how can I fix it? Thanks in advance.

ps. I could write at the top of my view something like this:

<% @uploads=Uploads.all%>

But that is not the best idea. Thanks in advance

4

3 回答 3

0

如果您的路由是 RESTful 路由,则更新方法是 HTTP PUT 方法,它不应该有模板。您正在寻找的是一个编辑操作:

def edit
    @uploads = Upload.all
end
于 2013-03-29T10:39:30.043 回答
0
<% @uploads.each do |upload| %>

<%end if @uploads.present? %>
于 2013-03-29T10:38:08.670 回答
0

@katja:参考您的评论。
update 是一个 HTTP PUT 请求,您无法在其中获取记录,但 edit 是一个获取请求,您可以在编辑操作中获取所有记录。

您可以将 before_filter 回调添加到您的编辑操作。

before_filter :get_all_uploads, only: [:edit]  

def get_all_uploads  
  @uploads = Upload.all  
end  

在你的 edit.html.erb 你可以做

<% @uploads.each do |upload| %>

    <p><%= upload.name %></p>               
    <p class="grey">

<%= best_in_place upload, :place, type: :select, collection: [ ["Home", "Home"],["Sauna", "Sauna"]]%>  

        </p>    
  <% end %>
于 2013-03-29T10:49:53.583 回答