Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Passing parameters to rails partials

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.84k
    Comment on it

    While rendering a partial if we want to send parameters to the partial we can do it like this:

    <%= render :partial => user_profile_list, :locals => { :user => @user } %>
    or
    <%= render :partial => "user_profile_list", :object => @user %>
    or
    <%= render "user_profile_list", :user => @user %>
    

    _user_profile.html.erb

    <div>
    <p><%= user.name%></p>
    <p><%= user.email %></p>
    </div>
    

    A smarter way to pass a collection as parameter to partial is:

    <%= render :partial => user_profile_list, :collection => @users %>
    

    _user_profile.html.erb

    <div>
    <p><%= user.name%></p>
    <p><%= user.email %></p>
    </div>
    

    The above html code will be repeated for all the user in the collection @users .

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: