Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 这是继续开发RubyonRails web应用程序的好方法吗?_Ruby On Rails - Fatal编程技术网

Ruby on rails 这是继续开发RubyonRails web应用程序的好方法吗?

Ruby on rails 这是继续开发RubyonRails web应用程序的好方法吗?,ruby-on-rails,Ruby On Rails,这是继续开发RubyonRails web应用程序的好方法吗 以下是注册样本: 用户模型:user.rb class User < ActiveRecord::Base attr_accessible :email, :name, :password, :username validates_presence_of :email, :name, :password, :username end 正确使用rails就是要遵循其惯例 将init更改为new 更改注册以创建 使用资源:路由的

这是继续开发RubyonRails web应用程序的好方法吗

以下是注册样本:

用户模型:user.rb

class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :username
validates_presence_of :email, :name, :password, :username
end

正确使用rails就是要遵循其惯例

  • 将init更改为new

  • 更改注册以创建

  • 使用资源:路由的用户

最好跟随railscast使用authLogic或Desive,而不是自己滚动。

给出了一些很好的建议,我会小心不要抄袭。然而,我想补充一些更具体的代码

为Rails 4.0做准备 我假设这是一个实践应用程序,或者是开发早期的东西。不管哪一个对你来说是正确的,我认为如果你继续采用Rails 4.0中的一些新变化,你会过得更好

新的变化之一是。Rails将不再在模型中使用
attr\u accessible
,并在控制器中创建一种处理质量分配的新方法。这将允许您“动态”更改哪些字段可以根据不同的用户或用户进行更新

到目前为止,更新您编写的代码并不困难。我已经着手做了关键的改变

Gemfile

gem 'strong_parameters'
用户模型:
user.php

class User < ActiveRecord::Base
  include ActiveModel::ForbiddenAttributesProtection
  validates_presence_of :email, :name, :password, :username
end
class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to login_path, :notice => "You have signed up successfully. You may now login!"
    else
      render :new, :alert => "There was an problem creating your account"
    end
  end

private

  def user_params
    params.require(:user).permit(:email, :name, :password, :username)
  end
end
注意:除了使用强参数之外,我还对
users\u controller.php
做了一些更改

  • 我更改了动作名称以匹配该动作
  • 我没有调用
    .valid?
    .save
    ,而是将
    if
    语句更改为仅使用
    .save
    。这是因为
    .save
    将调用
    .valid?
    并相应地返回false
  • 我修改/添加了对
    create
    操作的
    redirect_to
    render
    调用。
    redirect\u to
    调用是一个更重要的更改,因为它有助于防止双重表单提交
  • 在表单中使用分部代码 在Rails中使用视图的一个很酷的特性是。分部代码允许您提取在多个位置使用的HTML片段。通过使用分部代码,您可以在一个文件中为的
    表单_编写代码,并在
    新建
    编辑
    操作中使用它。保留您的代码

    另外,我注意到您没有在表单中使用
    标记。我认为这一点非常重要,因为它改善了用户体验

    注意:我已经更改了注册页文件名,以匹配我的
    users\u controller.php版本中的更改

    注册页面:
    new.html.erb

    <h1>Signup!!</h1>
    <%= render :partial => "form" %>
    
    <%= form_for @user do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %><br />
    
      <%= f.label :email, "Email Address" %>
      <%= f.email_field :email %><br />
    
      <%= f.label :username %>
      <%= f.text_field :username %><br />
    
      <%= f.label :password %>
      <%= f.password_field :password %><br />
    
      <%= f.submit "Sign Up" %>
    <% end %>
    

    如果您对我发布的内容有任何疑问,请询问。我会尽力解释我能解释的一切。

    您有更尖锐的问题吗?谢谢,但是控制器名称是否应该与型号名称相同?我是说user.rb和controller users\u controller.rb。非常感谢。但是控制器名称是否应该与模型名称相同?我的意思是user.rb与controller users\u controller.rb一起,而不是signin\u controller.rb???@user1664409是的,因为controller提供的操作与
    用户
    模型一起工作。
    new
    create
    操作将向数据库添加新用户。请看一下Ryan Bate关于从头开始构建身份验证的屏幕广播。您将看到,他使用
    userscoontroller
    进行注册,但他创建了一个
    SessionController
    来处理实际登录。他这样做是因为控制器和动作名称相互解释。
    class User < ActiveRecord::Base
      include ActiveModel::ForbiddenAttributesProtection
      validates_presence_of :email, :name, :password, :username
    end
    
    class UsersController < ApplicationController
      def new
        @user = User.new
      end
    
      def create
        @user = User.new(user_params)
        if @user.save
          redirect_to login_path, :notice => "You have signed up successfully. You may now login!"
        else
          render :new, :alert => "There was an problem creating your account"
        end
      end
    
    private
    
      def user_params
        params.require(:user).permit(:email, :name, :password, :username)
      end
    end
    
    <h1>Signup!!</h1>
    <%= render :partial => "form" %>
    
    <%= form_for @user do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %><br />
    
      <%= f.label :email, "Email Address" %>
      <%= f.email_field :email %><br />
    
      <%= f.label :username %>
      <%= f.text_field :username %><br />
    
      <%= f.label :password %>
      <%= f.password_field :password %><br />
    
      <%= f.submit "Sign Up" %>
    <% end %>