简体   繁体   English

RoR中有2个视图1个表单

[英]2 Views 1 Form in RoR

I am having trouble articulating the exact issue I am facing, but I will try with a brief descriptions and code. 我在表达确切的问题时遇到了麻烦,但是我将尝试使用简短的描述和代码。

I am trying to add a feature to a simple existing app that allows the user to crop an uploaded image for an avatar. 我正在尝试向一个简单的现有应用程序添加功能,以允许用户裁剪上传的图片以显示头像。 I do the file selection on the same view that allows the user to update their password and other various account options. 我在允许用户更新密码和其他各种帐户选项的同一视图上进行文件选择。 The user submits that form which then renders the view for the cropping feature. 用户提交该表格,然后呈现裁剪功能的视图。 The issue is that from the crop view, the submission fails because it fails validation of parameters from the previous form. 问题是,从农作物视图来看,提交失败,因为它无法验证先前表单中的参数。 Basically I would like the form to all be submitted at the same time but from two different views. 基本上,我希望所有表单都同时提交,但要有两种不同的观点。

user.rb user.rb

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :avatar,
                         :crop_x, :crop_y, :crop_w, :crop_h

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true

  mount_uploader :avatar, AvatarUploader
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
  after_update :crop_avatar

  def crop_avatar
    avatar.recreate_versions! if crop_x.present?
  end
end

I have tried several different things to remedy this. 我尝试了几种不同的方法来解决此问题。 I am sure I am missing a fundamental concept. 我确信我缺少一个基本概念。 Any ideas? 有任何想法吗?

users_controller.rb users_controller.rb

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    if params[:user][:avatar].present?
      render 'crop'
    else
      sign_in @user
      redirect_to @user, notice: "Successfully updated user."
    end
  else
   render 'edit'
  end
end

edit.html.erb edit.html.erb

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for @user, :html => {:multipart => true } do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

      <%= f.label :avatar %>
      <%= f.file_field :avatar %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

crop.html.erb crop.html.erb

<% provide(:title, 'Crop Avatar') %>
<h1>Crop Avatar</h1>

<div class="row">
  <div class="span6 offset3">
    <%= image_tag @user.avatar_url(:large), id: "cropbox" %>

    <h4>Preview</h4>
    <div style="width:100px; height:100px; overflow:hidden">
      <%= image_tag @user.avatar.url(:large), :id => "preview" %>
    </div>

    <%= form_for @user do |f| %>
      <% %w[x y w h].each do |attribute| %>
        <%= f.hidden_field "crop_#{attribute}" %>
      <% end %>
      <div class="actions">
        <%= f.submit "Crop" %>
      </div>
    <% end %>
  </div>
</div>

You can't have 2 views for one action. 一个动作不能有2个视图。 On a second thought why do you need it anyways, i mean you are rendering crop only when params[:user][:avatar] is present and that will be called only when you'll submit your edit.html.erb template. 再次思考为什么仍然需要它,我的意思是仅当存在params [:user] [:avatar]时才渲染作物,并且只有在您提交edit.html.erb模板时才会调用该作物。 I think what you can do is have another method in controller with name crop and there update user's avatar with the dimention's specified. 我认为您可以做的是在控制器中使用另一个名称为crop的方法,并使用指定的尺寸更新用户的头像。

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

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