Rails 4 - passing variable to partial

后端 未结 7 1355
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 03:49

I am following the Ruby on Rails tutorial and have come across a problem while trying to pass variables to partials.

My _user partial is as follows

相关标签:
7条回答
  • 2020-11-28 04:29

    ou are able to create local variables once you call the render function on a partial, therefore if you want to customize a partial you can for example render the partial _form.html.erb by:

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

    this way you can access in the partial to the label for the button and the URL, those are different if you try to create or update a record. finally, for accessing to this local variables you have to put in your code local_assigns[:button_label] (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%>
    
    0 讨论(0)
提交回复
热议问题