在Ruby on Rails中传递控制器和视图之间的模型

时间:2021-07-23 18:28:13

I have signup form on my home screen. If user inputs invalid data I redirect him to /signin page. On this page I can see filled fields, but errors descriptions are empty.

我在主屏幕上有注册表单。如果用户输入无效数据,我会将其重定向到/登录页面。在此页面上,我可以看到填充字段,但错误描述为空。

Here is my UsersController:

这是我的UsersController:

class UsersController < ApplicationController
  def new
    @user = User.new(params[:user])
  end

  def create
    @user = User.new(params[:user])
    print @user
    if  @user.save

    else
      render 'new'
    end
  end
end

Method I use to show errors

我用来显示错误的方法

module ApplicationHelper

  def errors_for(model, attribute)
    if model.errors[attribute].present?
      content_tag :div, :class => 'well error' do
        content_tag :ul do
          model.errors[attribute].collect {|item| concat(content_tag(:li, item))}

        end
      end
    end
  end
end

My form partial:

我的形式部分:

<%= f.label :user_name %>
<%= f.text_field :user_name, :class=>"input-medium" %>
<%= errors_for(@user, :user_name) %>

<%= f.label :email %>

<%= f.text_field :email, :class=>"input-medium " %>

<%= errors_for(@user, :email) %>

<%= f.label :password %>

<%= f.password_field :password, :class=>"input-medium" %>

<%= f.label :password_confirmation, "Confirmation" %>

<%= f.password_field :password_confirmation, :class=>"input-medium" %>

and my signup view:

和我的注册视图:

<section class="centered user-form-container">
  <div class="user-form well pull-left">
    <div class="centered">
      <h1>Sign up</h1>
      <%= form_for(@user, :action=>"create") do |f| %>
          <%= render 'signup', :f=>f %>
          <%= f.submit "Sign up" %>

      <% end %>
    </div>
  </div>

</section>

3 个解决方案

#1


1  

In this situation, I believe you need to use flash.now, something like this:

在这种情况下,我相信你需要使用flash.now,如下所示:

Per the rails docs:

根据rails docs:

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use flash.now in the same way you use the normal flash:

默认情况下,向Flash添加值将使它们可用于下一个请求,但有时您可能希望在同一请求中访问这些值。例如,如果创建操作无法保存资源并且您直接呈现新模板,那么这不会产生新请求,但您可能仍希望使用闪存显示消息。为此,您可以像使用普通闪存一样使用flash.now:

def create
  @user = User.new(params[:user])
  print @user
  if  @user.save

  else
    # start with this, then expand the error text
    flash.now[:error] = "Could not save user"
    render 'new'
  end

end

#2


1  

You would do this in your validation method.

您可以在验证方法中执行此操作。

If you are using a standard rails validation you would do this:

如果您使用的是标准rails验证,则可以执行以下操作:

validates_presence_of :foo, :message => 'Message you want to display here'

If you are doing a custom validation then this:

如果您正在进行自定义验证,那么:

def my_validation_method
  begin
    my_validation_code_here
  rescue
    self.errors[:base] << 'Message you want to display here'
  end
end

#3


1  

  def new
    @user = User.new(params[:user])
    if (!params[:user].nil?)
      @user.valid?
    end


  end

#1


1  

In this situation, I believe you need to use flash.now, something like this:

在这种情况下,我相信你需要使用flash.now,如下所示:

Per the rails docs:

根据rails docs:

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use flash.now in the same way you use the normal flash:

默认情况下,向Flash添加值将使它们可用于下一个请求,但有时您可能希望在同一请求中访问这些值。例如,如果创建操作无法保存资源并且您直接呈现新模板,那么这不会产生新请求,但您可能仍希望使用闪存显示消息。为此,您可以像使用普通闪存一样使用flash.now:

def create
  @user = User.new(params[:user])
  print @user
  if  @user.save

  else
    # start with this, then expand the error text
    flash.now[:error] = "Could not save user"
    render 'new'
  end

end

#2


1  

You would do this in your validation method.

您可以在验证方法中执行此操作。

If you are using a standard rails validation you would do this:

如果您使用的是标准rails验证,则可以执行以下操作:

validates_presence_of :foo, :message => 'Message you want to display here'

If you are doing a custom validation then this:

如果您正在进行自定义验证,那么:

def my_validation_method
  begin
    my_validation_code_here
  rescue
    self.errors[:base] << 'Message you want to display here'
  end
end

#3


1  

  def new
    @user = User.new(params[:user])
    if (!params[:user].nil?)
      @user.valid?
    end


  end