如何从Rails 3解决MultiparameterAssignmentErrors

时间:2015-03-17 09:59:33

标签: ruby ruby-on-rails-3

我在提交表单时收到以下错误。

错误:

ActiveRecord::MultiparameterAssignmentErrors in HomesController#savemagazines

1 error(s) on assignment of multiparameter attributes
Rails.root: C:/Site/library_management1

Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:85:in `new'
app/controllers/homes_controller.rb:85:in `savemagazines'

以下是我的代码片段。

的观点/舍/ magazines.html.erb:

<% if current_user %>
<div class="totaldiv">
  <div class="navdiv"><span>Purchase Magazines</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">Magazines Purchase</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
 </ul>  
</div>
 </div>
 <div class="restdiv" id="ex3" >
 <center>
 <div class="container">
    <%= form_for :magazines,:url => {:action => 'savemagazines'} do |f| %>
    <p>
      <label for="mg_name">Enter Magazine Name:</label>
      <%= f.select(:mag_name,options_for_select([['Business India ','Business India'],['Business Today ','Business Today '],['Forbes India ','Forbes India '],['Time magazine Asia ','Time magazine Asia '],['Electronics For You','Electronics For You']],selected: "magazine"), { include_blank: true }) %>
    </p>
    <p>
        <label for="mag_title">Enter Magazine Title</label>
        <%= f.text_field :mag_title,placeholder:"Enter magazine title" %>
    </p>
    <p>
        <%= f.check_box :cd_dvd,{},true,false %> CD/DVD
    </p>
    <p>
        <label for="purchase">Purchase Date</label>
        <%= f.date_select :purchased_on %>
    </p>
    <p>
        <%= f.submit "submit",:class => "btn btn-success" %>
    </p>
    <% 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 savebooks
        @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
        @magazines=Magazine.new(params[:magazines])
        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
end

20150317084229_create_magazines.rb

   class CreateMagazines < ActiveRecord::Migration
  def change
    create_table :magazines do |t|
      t.string :mag_name
      t.boolean :cd_dvd
      t.decimal :cost, :precision => 8, :scale => 2
      t.string :datetime
      t.string :mag_title

      t.timestamps
    end
  end
  def down
     create_table :magazines do |t|
       t.string :purchased_on
     end
  end
end

请检查我的代码,让我知道我在哪里犯了错误和解决方案。

0 个答案:

没有答案