Question

I used "rails generate scaffold project" to create an new web application. 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

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. 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)

# 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. I replaced the whole method with the following (this worked for my other application, but not this time on 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)

# 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?

Update 1

Here are the other methods that were automatically created by "rails generate scaffold" command

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

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
Was it helpful?

Solution

You just created a scaffold without any other fields. So your projects table simply has 3 default fields id, created_at and updated_at. Because of which in your update action when you do:

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. Look at your params hash from server log

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

It doesn't have a key 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?

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).

After this you would just need to update the project_params method as below:

def project_params
  params.require(:project).permit(:name, :duration)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top