[英]How to print multiple variable in Ruby controller?
I am totally new in ruby.我对 ruby 完全陌生。 My Ruby version 2.2.我的 Ruby 版本 2.2。 I have an edit profile form.我有一个编辑个人资料表格。 I want to show both the table and form data in two different different place.我想在两个不同的地方同时显示表格和表单数据。 Please check my code请检查我的代码
users_controller.rb用户控制器.rb
def edit_profile
@user = User.get_profile(session[:user_id])
raise $user.inspect
respond_to do |format|
if params[:user][:avatar]
params[:user][:photo] = orginal_file_upload params[:user][:avatar]
end
raise params.inspect
if @user.update(user_params)
format.html { redirect_to :back, notice: 'Your profile was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :my_profile }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Here I have define raise $user.inspect
and raise params.inspect
I want to get both data.在这里,我定义了raise $user.inspect
和raise params.inspect
我想获得这两个数据。 But here in my page only coming first one.但在我的页面中只有第一个。 Please check and let me know how to get my both array value.请检查并让我知道如何获取我的两个数组值。
Thank you.谢谢你。
raise
is a mechanism of throwing errors. raise
是一种抛出错误的机制。 raise $user.to_s
returns the control out of action. raise $user.to_s
返回失控的控制。 You can use puts
method to display the values in controller.您可以使用puts
方法来显示控制器中的值。
The correct code will be:正确的代码将是:
def edit_profile
@user = User.get_profile(session[:user_id])
respond_to do |format|
if params[:user][:avatar]
params[:user][:photo] = orginal_file_upload params[:user][:avatar]
end
@user.update(user_params)
format.html { render :inline => "User<p> ID:<%= @user.id %><br>NAME: <%= @user.name %><br>EMAIL: <%= @user.email %></p>Params <p><%= params.inspect%></p>".html_safe }
end
end
Assign them to instance variables like @user
and @params
and access them in your views.将它们分配给@user
和@params
等实例变量,并在您的视图中访问它们。 Having said that you already have assigned @user
variable and params
is accessible in your views automatically.话虽如此,您已经分配了@user
变量,并且params
可以在您的视图中自动访问。
# In your view
<%= @user.name %>
<%= params %>
Also, consider making your controller RESTful.另外,请考虑使您的控制器采用 RESTful。 You can send PUT
request to your controllers URL (eg. PUT profile/1
) it will automatically call your ProfileController#edit
method.您可以将PUT
请求发送到您的控制器 URL(例如PUT profile/1
),它会自动调用您的ProfileController#edit
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.