【发布时间】:2015-02-11 14:32:05
【问题描述】:
我有一个菜鸟问题。我需要另一个控制器的渲染视图。我使用设计并创建一个 perfil 控制器。
Perfil 控制器:
class PerfilController < ApplicationController
before_filter :authenticate_user!
def index
end
def show
@usuario = User.find(current_user)
@usuario.perfil ||= @usuario.build_perfil
@perfil = @usuario.perfil
end
def update
@usuario = User.find(current_user)
@perfil = Perfil.new(perfil_params)
@usuario.perfil ||= @usuario.build_perfil
respond_to do |format|
if @usuario.perfil.update_attributes(perfil_params)
format.html {redirect_to @usuario, notice: "update" }
else
format.html { render action: "show"}
end
end
end
private
def perfil_params
params.require(:perfil).permit(:nombre, :apellido)
end
end
用户控制器:
class UsersController < ApplicationController
before_filter :authenticate_user!
after_action :verify_authorized
def index
@users = User.all
authorize User
end
def show
@user = User.find(params[:id])
authorize @user
end
def update
@user = User.find(params[:id])
authorize @user
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
user = User.find(params[:id])
authorize user
user.destroy
redirect_to users_path, :notice => "User deleted."
end
private
def secure_params
params.require(:user).permit(:role)
end
end
class PerfilController < ApplicationController
before_filter :authenticate_user!
def index
end
def show
@usuario = User.find(current_user)
@usuario.perfil ||= @usuario.build_perfil
@perfil = @usuario.perfil
end
def update
@usuario = User.find(current_user)
@perfil = Perfil.new(perfil_params)
@usuario.perfil ||= @usuario.build_perfil
respond_to do |format|
if @usuario.perfil.update_attributes(perfil_params)
format.html {redirect_to @usuario, notice: "actualizado" }
else
format.html { render action: "show"}
end
end
end
private
def perfil_params
params.require(:perfil).permit(:nombre, :apellido)
end
end
表演/表演
<h2>Edit </h2>
<%= form_for @perfil, :url=>{:action=>:update}, :html=>{:method=>:put} do |f| %>
<p><%= f.label :nombres %><br />
<%= f.text_field :nombre %></p>
<div><%= f.label :apellidos %><br />
<%= f.text_field :apellido %></div>
<div><%= f.submit "Actualizar" %></div>
<% end %>
<%= link_to "Atrás", :back %>
好吧,我需要在用户/索引中显示更新
<h3>Configuracion de la cuenta</h3>
<div class = "col-md-3"> <h3>Users</h3>
<div class="nav nav-pills nav-stacked">
<%= render 'users/menu' %>
</div>
</div>
<div class = "col-md-7">
<h3>Informacion</h3>
<%= render 'users/show' %>
</div>
希望我解释清楚。
问候。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 devise ruby-on-rails-3.2