Raidspec Raidspec - 4 months ago 36
Ruby Question

Rails - Rendering form_for @user in a different view

I'm trying to render this code (partial to update users payment info).

/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>


I'm trying to get this partial to show up in a different page that's not under users_controller.rb, but I keep getting an error.

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>

Answer

In 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

In app/models/user.rb:

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

In app/controllers/orders_controller.rb:

class OrdersController < ApplicationController

  # assuming this 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

  # --OR--
  # assuming `current_user` is populated
  #
  def new
    @order = Order.new
    @user = current_user
  end


  private

  def new_params
    params.permit(:id)
  end
end

see: http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters


In 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} %>

see: http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

Comments