Chinu Chinu - 7 months ago 10
Ruby Question

How to print multiple variable in Ruby controller?

I am totally new in ruby. My Ruby version 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

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. But here in my page only coming first one. Please check and let me know how to get my both array value.

Thank you.

Answer

raise is a mechanism of throwing errors. raise $user.to_s returns the control out of action. You can use puts method to display the values in controller.

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