Rails - 在另一个视图中呈现form_for @user

时间:2016-07-04 10:01:12

标签: ruby-on-rails ruby devise

我正在尝试渲染此代码(部分更新用户付款信息)。

/devise/registrations/_credit.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<form action="#">
          <div class="form-group">
              <label class="control-label">Card Holder Name</label>
              <%= f.text_field :name, autofocus: true, class:"form-control" %></div>
            <div class="form-group">
                <label class="control-label">Card Number</label>
                <%= f.text_field :card, autofocus: false, class:"form-control" %>
            </div>
            <div class="form-group">
                <label class="control-label">CVV</label>
                <%= f.text_field :cvv, autofocus: false, class:"form-control" %>
            </div>
            <div class="form-group">
                <label class="control-label">Expiration Month</label>
                <%= f.text_field :expmonth, autofocus: false, class:"form-control" %>
            </div>
            <div class="form-group">
                <label class="control-label">Expiration Year</label>
                <%= f.text_field :expyear, autofocus: false, class:"form-control" %>
            </div>
            <div class="margin-top-10">
                 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
              <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
            <% end %>

            <div class="actions">
              <%= f.submit "Update", class:"btn green" %>
              <% end %>
            </div>
    </form>
</div>

我试图让这个部分显示在不在users_controller.rb下的不同页面中,但我一直收到错误。

NameError in Orders#new
Showing /home/nitrous/code/uvesty/app/views/devise/registrations/_credit.html.erb where line #2 raised:

undefined local variable or method `resource' for #<#<Class:0x00563138834928>:0x00563138821670>

1 个答案:

答案 0 :(得分:2)

app/models/order.rb

class Order < ActiveRecord::Base
  belongs_to :user

  # only use this if user instance is created before order, 
  # and alway associated with order
  validates :user, presence: true
end

app/models/user.rb

class User < ActiveRecord::Base
  has_many :orders
  # --OR--
  # has_one :order
end

app/controllers/orders_controller.rb

class OrdersController < ApplicationController
  # 1)assuming `current_user` is populated
  #
  def new
    @order = Order.new
    @user = current_user
  end

  # --OR--
  # 2) assuming the following line is in `config/routes.rb`:
  #   get '/orders/new/:id' => 'orders#new'
  #
  def new
    if @order = Order.find_by_id(new_params[:id])
      @user = @order.user

    else
      # do whatever here; replace the next line
      raise "order not found"
    end
  end

  private

  # only needed for 2) above
  def new_params
    params.permit(:id)
  end
end

请参阅:http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

app/view/orders/new.html.erb

<% raise('@user not defined') unless @user %><%# <- Remove this line after partial is working %>
<%= render partial: "devise/registrations/credit", locals: {resource: @user, resource_name: :user} %>

请参阅:http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables