如何使用rails 3解决ArgumentError?

时间:2015-03-27 06:46:01

标签: ruby-on-rails ruby ruby-on-rails-3

我正在尝试在我的应用中编辑个人资料。当我插入新设置并更新它时,它会给我以下错误。

错误:

wrong number of arguments (1 for 0)
Rails.root: C:/Site/library_management1

Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:148:in `updatedata' 

我的代码如下

的观点/舍/ userprofile.html.erb

<% if current_user %>
<div class="totaldiv">
  <div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
  <div class="wrapper">
  <div id="leftsidebtn">
 <ul>
  <li><a href="/homes/issuebooks">Book issue</a></li>
   <li><a href="/homes/availablebooks">Books Available</a></li>
   <li><a href="/homes/magazines?user_id=<%= current_user.id %>">Magazines Purchase</a></li>
   <li><a href="/homes/newspaper">Newspaper Purchase</a></li>
   <li><a href="/homes/userprofile">Profile settings</a></li>
   <li><a href="/homes/blog">My Blog</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
 </ul>  
</div>
  </div>
  <div class="restdiv" id="ex3" >
<center>
  <div class="edit-profile"><button type="button" class="btn btn-success" id="btnShowModal" >Edit Your Profile</button></div>
  <div id="output"></div>

<div id="overlay" class="web_dialog_overlay"></div>
<div id="dialog" class="web_dialog">
<div class="edit-firstdiv">
  <div class="web_dialog_title align_right">
            <a href="#" id="btnClose">Close</a>
  </div>
  <%= form_for :users,:url => {:action => 'updatedata',:id => params[:id] } do |f| %>
  <% if @users.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@users.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @users.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<div class="edit-firstname">
<label for="first_name">First Name</label>
<%= f.text_field :first_name,:id => 'first_name',:value => @users.first_name %>
</div>
<div class="edit-lastname">
<label for="last_name">Last Name</label>
<%= f.text_field :last_name,:id => 'first_name',:value => @users.last_name %>
</div>
<div class="edit-emailid">
<label for="emailid">Emailid</label>
<%= f.email_field :email,:id => 'first_name',:value => @users.email %>
</div>
<div class="edit-password">
<label for="password">Password</label>
<%= f.password_field :password,:id => 'first_name',:value => @users.password %>
</div>
<div class="edit-confirm">
<label for="Address">Password again</label>
<%= f.password_field :password_confirmation,:id => 'first_name' %>
</div>
<div class="edit-telephone">
<label for="phone">Phone no</label>
<%= f.telephone_field :tel_no,:id => 'first_name',:value => @users.tel_no %>
</div>
<div class="edit-address">
<label for="Address">Address</label>
<%= f.text_area :address,:class => 'address_text',:value => @users.address %>
<textarea id="first_name"></textarea>
</div>
<div class="edit-submit">
  <%= f.submit 'Update Data',:class => 'btn btn-success' %>
</div>
</div>
<% end %>
  </div>
</center>
  </div>
</div>
<% end %>

控制器/ homes_controller.rb

class HomesController < ApplicationController
     before_filter :authenticate_admin!,only: [:admin]
    def index

    end
    def admin

    end
    def managebooks
        @books=Book.new
        if params[:id]
            @books=Book.find(params[:id])
            @book=Book.all
        end
    end
    def savebooks
        @books=Book.new(params[:books])
        if @books.save
            flash[:notice]="Data has submitted successfully"
            flash[:color]="valid"
            redirect_to :action => 'managebooks',:id => @books.id
        else
            flash[:notice]="Data couldnot submitted successfully"
            flash[:color]="invalid"
            render 'managebooks'
        end
    end
    def remove
        @books=Book.find(params[:id])
        @books.destroy
    end
    def books

    end
    def showbooks
        @books=Book.all
    end
    def searchbooks
        @books=Book.all
    end
    def member
        @users=User.new
    end
    def registration
        @users=User.new
    end
    def savedata
        @users=User.new(params[:users])
        if @users.save
            flash[:notice]="Data has submitted successfully"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:notice]="Data could not submitted successfully"
            flash[:color]="invalid"
            render 'registration'
        end
    end
    def issuebooks
        @issues=Issue.new
    end
    def savedissuebooks
        @issues=Issue.new(params[:issues])
        if @issues.save
            flash[:notice]="information has saved successfully"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:notice]="Data couldnot saved"
            flash[:color]="invalid"
            render 'issuebooks'
        end
    end
    def availablebooks

        @books=Book.all
    end
    def userissues
        @issues=Issue.all
    end
    def magazine
        @magazines=Magazine.new
    end
    def savemagazines
        @users=User.find(params[:id])
        @magazines=Magazine.new(params[:magazines])
        @magazines.user_id=@users.id
        if @magazines.save
            flash[:notice]="Data submitted successfully"
            flash[:color]="valid"
            redirect_to :action => "member"
        else
            flash[:notice]="Data could not saved"
            flash[:color]="invalid"
            render 'magazines'
        end
    end
    def magazineissue
        @magazines=Magazine.all
        @users=User.find @magazines.first.user_id
    end
    def blog
        @blogs=Blog.new
    end
    def savecomments
        @users=User.find(params[:id])
        @blogs=Blog.new(params[:blogs])
        @blogs.user_id=@users.id
        if @blogs.save
            flash[:notice]="Comment has been posted successfully"
            flash[:color]="valid"
            redirect_to :action => "showcomment"
        else
            flash[:notice]="Comment could not saved"
            flash[:color]="invalid"
            render 'blog'
        end
    end
    def showcomment
        @blogs=Blog.all
    end
    def newspaper
        @newspapers=Newspaper.new
    end
    def savenewspaper
        @users=User.find(params[:id])
        @newspapers=Newspaper.new(params[:newspapers])
        @newspapers.user_id=@users.id
        if @newspapers.save
            flash[:notice]="newspaper data saved successfully"
            flash[:color]="valid"
            redirect_to :action => "member"
        else
            flash[:alert]="Data could not saved successfully"
            flash[:color]="invalid"
            render 'newspaper'
        end
    end
    def adminnewspaperissue
        @newspapers=Newspaper.all
        @users=User.find @newspapers.first.user_id
    end
    def userprofile
        @users=User.find(params[:id])
    end
    def updatedata
        @users=User.find(params[:id])
        if @users.update_attributes(params(:users))
            flash[:notice]="User Data has updated"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:alert]="Data could not updated"
            flash[:color]="invalid"
            render 'userprofile'
        end
    end
end

请检查我的代码,让我知道我错误的地方和解决方案。

2 个答案:

答案 0 :(得分:1)

调试参数错误时,您有两种可能的选择:

  1. 你传递给函数的很多或很少的参数。
  2. 你做了一个令人困惑的错字。
  3. 首先要做的是查看提供的堆栈跟踪。它会告诉你要看。最有可能的错误是在跟踪中给出的行或其正上方的行中的错误。在你的情况下它说:

    app / controllers / homes_controller.rb:148:在`updatedata&#39;

    这告诉你要查看:

    controller:homes_controller.rb

    行动:updatedata

    确切行号:148

    您的代码编辑器应该能够显示行号和/或跳转到特定的行号。如果仔细查看该位置,您将看到使用()而不是[]的情况。我怀疑这会使系统混乱并导致参数错误。

    此外,从轨道编码标准的角度来看,您应该将该控制器分解为多个控制器。通常,您应该只在控制器中执行索引,新建,创建,编辑,更新和删除操作。如果您发现自己制作了许多自定义命名操作,则表明您需要另一个控制器。

答案 1 :(得分:0)

params是一个哈希值,因此您必须将第148行编辑为:

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