Rails edit.html.erb ArgumentError

时间:2014-11-13 20:50:49

标签: ruby-on-rails

rails的新手,我参加了Hartl教程并尝试进行一些更改。我想允许用户编辑帖子(我正在调用路由),并在编辑路径时遇到此错误:

else
      object      = record.is_a?(Array) ? record.last : record
      raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
      object_name = options[:as] || model_name_from_record_or_class(object).param_key
      apply_form_for_options!(record, object, options)
    end

控制器:

class RoutesController < ApplicationController
   # before_action :logged_in_user#, only: [:create, :destroy, :edit, :new]

  def index
    @routes = Route.paginate(page: params[:page])
  end
  
  def create
    @routes = current_user.routes.build(route_params)
    if @routes.save
      flash[:success] = "Route created!"
      redirect_to '/routes#index'
    else
      render 'new'
    end
  end

  def destroy
  end
  
  def show
    @routes = Route.find(params[:id])
  end
  
  def edit
  end
  
  def update
    if @routes.update_attributes(routes_params)
      flash[:success] = "Route updated"
      redirect_to @routes
    else
      render 'edit'
    end
  end 

  private

    def route_params
      params.require(:route).permit(:title, :content, :picture)
    end
    
end

这是edit.html.erb文件

<% provide(:title, "Edit route") %>
<h1>Update your route</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for @route do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :title %>
      <%= f.text_field :title, class: 'form-control' %>

      <%= f.label :content %>
      <%= f.text_field :content, class: 'form-control' %>
      
      <h4>Change your image.</h4>
      <span class="picture">
        <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
      </span>

      <%= f.submit "Save changes", class: "btn btn-primary" %>
    <% end %>

   
  </div>
</div>

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您必须在控制器的编辑操作中设置@route变量。

def edit
      @route = Route.find(params[:id])
end

应该这样做。