简体   繁体   English

如何在Ruby控制器中打印多个变量?

[英]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.inspectraise 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.

相关问题 如何通过使用Ruby on Rails在控制器中打印数据? - How to print data in controller by using Ruby on Rails? 如何在 Ruby 代码中打印 JavaScript 变量 - How to print a JavaScript variable within Ruby code 如何在 Ruby on Rails 中访问 1 个 controller 中的多个模型 - How to access multiple models in 1 controller in Ruby on Rails 在 Ruby on Rails 上,我们如何在控制器内打印调试信息? - On Ruby on Rails, how do we print debug info inside of a controller? Ruby on Rails:如何在视图中打印变量内容并选中复选框? - Ruby on Rails: How to print contents of variable in view and make checkbox? 在Ruby on Rails中,如何将实例变量添加到类中(并在控制器中以JSON形式返回)? - In Ruby on Rails, how to add an instance variable to a class (and return as JSON in controller)? Ruby on Rails:如何在控制器函数之间传递变量? - Ruby on Rails: How to pass variable between controller functions? 如何在Ruby on Rails中执行期间确定控制器变量的值? - How to determine the value of a controller variable during execution in Ruby on Rails? 如何从视图中修改控制器中的变量(红宝石在轨道上) - How can I modify a variable in the controller from the view (ruby on rails) Ruby On Rails控制器中的实例变量 - Instance variable in controller with Ruby On Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM