简体   繁体   English

Windows上的rails生成scaffold命令导致ArgumentError

[英]rails generate scaffold command on Windows causes ArgumentError

I used "rails generate scaffold project" to create an new web application. 我使用“ rails generate scaffold project”创建了一个新的Web应用程序。 I already did this in the past on Linux and Mac OSX running other versions of rails and ruby and all worked fine, but this time I'm working on Windows 7. Here is my environment 过去,我已经在运行其他版本的rails和ruby的Linux和Mac OSX上进行了此操作,并且一切正常,但是这次我在Windows 7上工作。这是我的环境

C:\Users\user1\Company>ruby -v
ruby 2.0.0p451 (2014-02-24) [x64-mingw32]

C:\Users\user1\Company>rails -v
DL is deprecated, please use Fiddle
Rails 4.1.0

C:\Users\user1\Company>

after I ran the scaffold command, I ran rake db:migrate and I was able to create my first project successfully. 在运行scaffold命令之后,我运行了rake db:migrate,并且能够成功创建我的第一个项目。 Then I can edit the project, but when I click update, I get the following error message 然后,我可以编辑项目,但是当我单击更新时,出现以下错误消息

ArgumentError (When assigning attributes, you must pass a hash as an argument.):

Full server log message 完整的服务器日志消息

Started PATCH "/projects/1" for 127.0.0.1 at 2014-04-29 05:16:33 -0700
Processing by projectsController#update as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}
  project Load (0.0ms)  SELECT  "projects".* FROM "projects"  WHERE "projects"."id"= ? LIMIT 1  [["id", 1]]
   (1.0ms)  begin transaction
   (0.0ms)  rollback transaction
Completed 500 Internal Server Error in 10ms

ArgumentError (When assigning attributes, you must pass a hash as an argument.):

  app/controllers/projects_controller.rb:44:in `block in update'
  app/controllers/projects_controller.rb:43:in `update'

  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (104.0ms)

Here is my "update" method (as was created automatically by the scaffold command) 这是我的“更新”方法(由scaffold命令自动创建)

# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
  respond_to do |format|
    if @project.update(project_params)
      format.html { redirect_to @project, notice: 'Update Successful!' }
      format.json { render :show, status: :ok, location: @project }
    else
      format.html { render :edit }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

I tried removing the "PATCH/" keyword, but no luck. 我尝试删除了“ PATCH /”关键字,但是没有运气。 I replaced the whole method with the following (this worked for my other application, but not this time on Windows) 我将整个方法替换为以下内容(此方法适用于我的其他应用程序,但这次不适用于Windows)

# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
  @project = Project.find(params[:id])

  if @project.update_attributes(params[:project])
    flash[:notice] = "Update Successful!"
  end
  respond_with(@project)
end

but this did not make any difference. 但这没有任何区别。

I also tried (I found this by browsing SO) 我也尝试过(我是通过浏览找到的)

# PUT /projects/1
# PUT /projects/1.json
def update
  @project = Project.find(params[:id])

  respond_to do |format|
    if @project.update_attributes(params[:project])
      format.html { redirect_to @project, notice: 'Update Successful!' }
      format.json { head :ok }
    else
      format.html { render action: "edit" }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end

but no success either 但也没有成功

Here are the other working methods (all as automatically generated by "rails generate scaffold" command, and they all work fine) 这是其他工作方法(全部由“ rails generate scaffold”命令自动生成,并且都可以正常工作)

# GET /projects
# GET /projects.json
def index
  @projects = project.all
end

# GET /projects/1
# GET /projects/1.json
def show
end

# GET /projects/new
def new
  @project = project.new
end

# GET /projects/1/edit
def edit
end

# POST /projects
# POST /projects.json
def create
  @project = project.new(project_params)

  respond_to do |format|
    if @project.save
      format.html { redirect_to @project, notice: 'Creation Successful!' }
      format.json { render :show, status: :created, location: @project }
    else
      format.html { render :new }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

Why is "rails generate scaffold" command not working on Windows and working fine on Linux and Mac OSX? 为什么“ rails generate scaffold”命令在Windows上不起作用,而在Linux和Mac OSX上却不能正常工作?

Update 1 更新1

Here are the other methods that were automatically created by "rails generate scaffold" command 这是“ rails generate scaffold”命令自动创建的其他方法

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  <other methods listed above : index, show, new, edit, create, update, and destroy>

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params[:project]
    end
end

Working Code after making the changes suggested by Kirti Thorat 进行Kirti Thorat建议的更改后的工作代码

This is what worked for me 这就是对我有用的

# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update

@project = Project.find(params[:id])

  respond_to do |format|
    if @project.update(project_params)
      format.html { redirect_to @project, notice: 'Project was successfully updated.' }
      format.json { render :show, status: :ok, location: @project }
    else
      format.html { render :edit }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_project
    @project = Project.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def project_params
    params.require(:project).permit(:UnitMgtAddress)
  end

You just created a scaffold without any other fields. 您刚刚创建了一个没有任何其他字段的支架。 So your projects table simply has 3 default fields id , created_at and updated_at . 因此,您的projects表仅具有3个默认字段idcreated_atupdated_at Because of which in your update action when you do: 因此,在执行update操作时,您需要执行以下操作:

if @project.update(project_params)

Or 要么

if @project.update_attributes(params[:project])

you get error as update and update_attributes require a Hash as an argument and params[:project] is nil. 由于updateupdate_attributes需要将Hash作为参数,并且params[:project]为nil,因此会出现错误。 Look at your params hash from server log 查看服务器日志中的params哈希

{"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}

It doesn't have a key project . 它没有关键project

Possible Solutions 可能的解决方案

Without adding new fields 无需添加新字段

If you are not planning to add any new fields in your projects table then there is no point in having an update action as what field would you update on? 如果您不打算在projects表中添加任何新字段,那么进行update操作就没有意义,因为您将在哪个字段上进行更新?

With adding new fields 随着添加新字段

You can add new fields to your projects table like name , duration , etc as per your requirement (By creating a migration to add new fields). 您可以根据需要将新字段添加到projects表中,例如nameduration等(通过创建迁移以添加新字段)。

After this you would just need to update the project_params method as below: 之后,您只需要更新project_params方法,如下所示:

def project_params
  params.require(:project).permit(:name, :duration)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM