복붙노트

[RUBY-ON-RAILS] (4) 레일 - 부분적으로 가변 전달

RUBY-ON-RAILS

(4) 레일 - 부분적으로 가변 전달

나는 루비 온 레일즈 튜토리얼을 다음 오전 파셜에 변수를 전달하는 동안 문제를 가로 질러왔다.

다음과 같은 부분 내 _user입니다

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
</li>

나는 크기 값에 숫자를 전달하고 싶습니다. 운이없이 다음과 같이 나는 노력하고 있습니다.

<%= render @users, :locals => {:size => 30} %>

해결법

  1. ==============================

    1.당신은 지역 주민을 통과하는 경우는 전체 렌더링 부분 구문이 필요합니다

    당신은 지역 주민을 통과하는 경우는 전체 렌더링 부분 구문이 필요합니다

    <%= render @users, :locals => {:size => 30} %>
    

    이되다

    <%= render :partial => 'users', :collection => @users, :locals => {:size => 30} %>
    

    또는 새로운 해시 구문을 사용합니다

    <%= render partial: 'users', collection: @users, locals: {size: 30} %>
    

    어떤 내가 생각하는 더 많은 읽을 수 있습니다

  2. ==============================

    2.PartialRender에 레일 API의 :

    PartialRender에 레일 API의 :

    예를 들면 :

    # Instead of <%= render partial: "account" %>
    <%= render "account" %>
    
    # Instead of <%= render partial: "account", locals: { account: @buyer } %>
    <%= render "account", account: @buyer %>
    
    # @account.to_partial_path returns 'accounts/account', so it can be used to replace:
    # <%= render partial: "accounts/account", locals: { account: @account} %>
    <%= render @account %>
    
    # @posts is an array of Post instances, so every post record returns 'posts/post' on `to_partial_path`,
    # that's why we can replace:
    # <%= render partial: "posts/post", collection: @posts %>
    <%= render @posts %>
    

    그래서, 당신은 다음과 같이 렌더링하는 지역 변수의 크기를 통과 사용할 수 있습니다 :

    <%= render @users, size: 50 %>
    

    다음 _user.html.erb 부분에서 사용 :

    <li>
        <%= gravatar_for user, size: size %>
        <%= link_to user.name, user %>
    </li>
    

    그 크기를 참고 : 크기 => 크기 : 크기는 동일합니다.

  3. ==============================

    3.어느 한 쪽

    어느 한 쪽

    render partial: 'user', locals: {size: 30}
    

    또는

    render 'user', size: 30
    

    사용 지역 주민에게, 당신은 부분이 필요합니다. 부분 인수하지 않고, 그냥 직접 변수를 나열 할 수 있습니다 (안 지역 주민 이내)

  4. ==============================

    4.대신 30 : 레일 4.2에서 나는 지역 주민의 부분 만 사용 크기를 제거했다. 그렇지 않으면 제대로 로컬 변수를 전달하지 않을 것입니다.

    대신 30 : 레일 4.2에서 나는 지역 주민의 부분 만 사용 크기를 제거했다. 그렇지 않으면 제대로 로컬 변수를 전달하지 않을 것입니다.

    예를 들어, 이것을 사용 :

    <%= render @users, size: 30 %>
    
  5. ==============================

    5.; 당신이 다음 렌더링 escape_JavaScript을 (를) 사용하려면 JavaScript를 사용하는 경우 ( "{@ 된 newval => @ 인 oldval} %> 을")

    ; 당신이 다음 렌더링 escape_JavaScript을 (를) 사용하려면 JavaScript를 사용하는 경우 ( "{@ 된 newval => @ 인 oldval} %> 을")

  6. ==============================

    6.구문 다른 작은하지만 내 의견 청소기 같습니다

    구문 다른 작은하지만 내 의견 청소기 같습니다

    render 'my_partial', locals: { title: "My awesome title" }
    
    # not a big fan of the arrow key syntax
    render 'my_partial', :locals => { :title => "My awesome title" }
    
  7. ==============================

    7.당신은 당신이 예를 들어 의해 부분 _form.html.erb을 렌더링 할 수있는 부분을 사용자 정의하려는 경우 OU는 부분에 기능을 렌더링 호출하면 따라서 지역 변수를 만들 수 있습니다 :

    당신은 당신이 예를 들어 의해 부분 _form.html.erb을 렌더링 할 수있는 부분을 사용자 정의하려는 경우 OU는 부분에 기능을 렌더링 호출하면 따라서 지역 변수를 만들 수 있습니다 :

    <%= render 'form', button_label: "Create New Event", url: new_event_url %>
    <%= render 'form', button_label: "Update Event", url: edit_event_url %>
    

    만들거나 레코드를 업데이트하려고하면 버튼과 URL에 대한 레이블 부분에 액세스 할 수있는이 방법은, 사람들은 다르다. [: BUTTON_LABEL] 마지막으로,이 지역 변수에 액세스하기 위해 당신은 당신의 코드 local_assigns에 넣어야 할 (local_assigns [: name_of_your_variable])

    <%=form_for(@event, url: local_assigns[:url]) do |f|  %>
    <%= render 'shared/error_messages_events' %>
    <%= f.label :title ,"Title"%>
      <%= f.text_field :title, class: 'form-control'%>
      <%=f.label :date, "Date"%>
      <%=f.date_field :date, class: 'form-control'  %>
      <%=f.label :description, "Description"%>
      <%=f.text_area :description, class: 'form-control'  %>
      <%= f.submit local_assigns[:button_label], class:"btn btn-primary"%>
    <%end%>
    
  8. from https://stackoverflow.com/questions/16242121/rails-4-passing-variable-to-partial by cc-by-sa and MIT license