2017-04-25 1 views
0

habe ich haben zwei Formen:Wie für 2 bildet teilweise zu bauen, die verschiedene URL-Pfade

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1> 

<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@user) do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= f.label :name %> 
     <%= f.text_field :name, class: 'form-control' %> 

     <%= f.label :email %> 
     <%= f.email_field :email, class: 'form-control' %> 

     <%= f.label :password %> 
     <%= f.password_field :password, class: 'form-control' %> 

     <%= f.label :password_confirmation, "Confirmation" %> 
     <%= f.password_field :password_confirmation, class: 'form-control' %> 

     <%= f.submit "Save changes", class: "btn btn-primary" %> 
    <% end %> 

    <div class="gravatar_edit"> 
     <%= gravatar_for @user %> 
     <a href="http://gravatar.com/emails" target="_blank">change</a> 
    </div> 
    </div> 
</div> 

und

<% provide(:title, 'Sign up') %> 
<h1>Sign up</h1> 

<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@user, url: signup_path) do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= f.label :name %> 
     <%= f.text_field :name, class: 'form-control' %> 

     <%= f.label :email %> 
     <%= f.email_field :email, class: 'form-control' %> 

     <%= f.label :password %> 
     <%= f.password_field :password, class: 'form-control' %> 

     <%= f.label :password_confirmation, "Confirmation" %> 
     <%= f.password_field :password_confirmation, class: 'form-control' %> 

     <%= f.submit "Create my account", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

ich einen Teil erstellen müssen, was ich getan habe:

Aber es gibt ein Problem, dass Linien <%= form_for(@user) do |f| %> und <%= form_for(@user, url: signup_path) do |f| %> sind etwas anders - man übergibt eine URL an die für M Helfer der andere nicht.

Section 10.1.1 von Rails-Tutorial schlägt vor, es gibt eine Möglichkeit, es mit der provide Methode ("Mein Vorschlag ist die Variable-Passing-Technik"), aber ich konnte es nicht finden. Ich habe versucht, <% provide(:link, signup_path) und dann <%= form_for(@user, url: yield(:link)) do |f| %> zu übergeben, aber es hat nicht funktioniert. This answer bot nicht die Lösung, nach der Hartl suchen würde.

Dank

Antwort

1

Eine Möglichkeit, es zu tun ist, eine Instanzvariable zu verwenden, um Ihre Teil wissen zu lassen, ob es sich um den Standardpfad oder signup_path verwenden sollten.

# In your controller's action, define the following instance variable only if you want form_for's url to be signup_path 
def whatever_action 
    @replace_form_path = true 
end 

# In your view 
form_for(@user, (instance_variable_defined?(:@replace_form_path) ? {url: signup_path} : {})) do 
end 
+0

Es funktioniert! Vielen Dank! – Pere

Verwandte Themen