简体   繁体   English

试图在Ruby on Rails中使用表单提交创建模型对象

[英]trying to create model object with form submission in Ruby on Rails

First time with ROR and I'm trying to create a new model object from the index view on form submission. 第一次使用ROR,我正在尝试从表单提交的索引视图创建一个新的模型对象。 I think I got it figured out, but I'm getting: 我想我已经知道了,但是我得到了:

uninitialized constant UsersController

In my view I have: 我认为我有:

=form_for :user, :url => { :action => "new", :controller => "user" } do |f|
    = f.text_field :username, :placeholder => 'Username', :size => 30
    %br
    = f.text_field :email, :placeholder => 'E-mail', :size => 30
    %br
    = f.password_field :password, :placeholder => 'Password', :size => 30
    %br
    %br
    = f.submit_tag 'Sign Up'

In my controller I have: 在我的控制器中,我有:

def new
user_info = :params[:user]
    @user = User.create!(user_info)
    flash[:notice] = "#{@user.username} has been added!  Please fill out your profile page."
    redirect_to user_path
end

Can anyone see why I'm getting this? 谁能看到我为什么得到这个?

First issue was just using f.submit_tag with a form_for helper. 第一个问题是仅将f.submit_tagform_for辅助f.submit_tag一起使用。 Other comments.. 其他的建议..

When doing forms like this in Rails you should use the new action to setup an empty model for the form to work on 在Rails中执行这样的表单时,您应该使用new操作为表单创建一个空模型

def new
  @user = User.new

  respond_to do |format|
    format.html  # new.html.erb
    format.json  { render :json => @user }
  end
end

The form will then submit to the create action, where you read the params and use those to create a User object that you'll save 然后,表单将提交到create动作,您在其中读取params并使用这些params创建要保存的User对象

def create
  @user = User.new(params[:post])

  respond_to do |format|
    if @user.save
      format.html  { redirect_to(@user, :notice => 'User was successfully created.') }
      format.json  { render :json => @user, :status => :created, :location => @user }
    else
      format.html  { render :action => "new" }
      format.json  { render :json => @post.errors, :status => :unprocessable_entity }
    end
  end
end

The respond_to blocks here let you check for additional file extensions, usually .json or .xml, and give you a way to format your output for each of those extensions. 此处的respond_to块使您可以检查其他文件扩展名,通常是.json或.xml,并为您提供了一种格式来格式化每个扩展名的输出。

For the show action you could have the following: 对于show动作,您可以具有以下内容:

def show
  @user = User.find(params[:id])

  respond_to do |format|
    format.html  # show.html.erb
    format.json  { render :json => @user }
  end
end

and go to /users/1 you would see the your view rendered (this is the default thus the empty line there with the comment #show.html.erb you don't have to do anything assuming you have a view) 并转到/ users / 1,您将看到呈现的视图(这是默认视图,因此带有注释#show.html.erb的空行#show.html.erb您无需执行任何操作即可)

and if you go to /users/1.json you'll get just the result of @user.to_json which is useful for talking to Rails from your Javascript 如果您转到/users/1.json,您将只得到@user.to_json的结果,这对于从Javascript与Rails对话非常有用

And regarding the undefined constant issue: 关于未定义的常量问题:

  1. Check that your UsersController isn't missing any end lines 检查您的UsersController是否没有任何end线

  2. Check that the relevant User model and UsersController have all the correct naming conventions so as not to confuse Rails 检查相关的User模型和UsersController是否具有所有正确的命名约定,以免混淆Rails

users_controller.rb: users_controller.rb:

class UsersController < ApplicationController
...
end

user.rb: user.rb:

class User < ActiveRecord::Base
...
end

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

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