简体   繁体   中英

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.

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

Assign them to instance variables like @user and @params and access them in your views. Having said that you already have assigned @user variable and params is accessible in your views automatically.

# In your view
<%= @user.name %>

<%= params %>

Also, consider making your controller RESTful. You can send PUT request to your controllers URL (eg. PUT profile/1 ) it will automatically call your ProfileController#edit method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM