如何解决Type错误?

时间:2015-01-23 09:39:37

标签: ruby ruby-on-rails-3

我正在使用Rails版本3.2.19

错误:

TypeError in UsersController#create

can't convert Symbol into String
Rails.root: C:/Site/myapp1

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:25:in `user_params'
app/controllers/users_controller.rb:12:in `create'

我的代码片段如下所示。

视图/用户/ index.html.erb

<center>
  <h1>Select your option</h1>
  <p>
    <%= link_to "Register here",users_new_path %>
  </p>
  <p>
    <%= link_to "Display your data",users_show_path %>
  </p>
</center>

视图/用户/ new.html.erb

<h1>Enter your data</h1>
<center>
  <%= form_for :user,:url => {:action => 'create'} do |f| %>
      <% if @user.errors.any? %>
          <div id="errorExplanation">
            <h2><%= pluralize(@user.errors.count, "error") %> prohibited
              this post from being saved:</h2>
            <ul>
              <% @user.errors.full_messages.each do |msg| %>
                  <li><%= msg %></li>
              <% end %>
            </ul>
          </div>
      <% end %>

      <p>
        <%= f.label :Name %>
        <%= f.text_field :name,placeholder:"Enter your name" %>
      </p>
      <p>
        <%= f.label :Email %>
        <%= f.email_field :email,placeholder:"Enter your email" %>
      </p>
      <p>
        <%= f.label :content %>
        <%= f.text_area :content %>
      </p>
      <p>
        <%= f.submit "Create" %>
      </p>
  <% end %>
</center>

控制器/ users_controller.rb

class UsersController < ApplicationController
  def index

  end
  def new
    @user=User.new
  end
  def show

  end
  def create
    @user=User.new(user_params)
    if @user.save
      flash[:notice]="User has created successfully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="User did not create"
      flash[:color]="invalid"
      render :new
    end
  end
  private
  def user_params
    params.require(:user).permit(:name, :email,:content)
  end
end

模型/ user.rb

class User < ActiveRecord::Base
  attr_accessible :content, :email, :name
  EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
end

当我点击“创建”按钮时,我收到上述错误。请帮忙解决这个错误。

1 个答案:

答案 0 :(得分:1)

您似乎尚未配置 strong_parameters

您可以在https://github.com/rails/strong_parameters

找到更多相关信息