Partial transition variable

I am learning Ruby on Rails using book . I am stuck in the second exercise here .

My partial form app/views/users/_form.html.erb looks like this:

 <%= form_for(@user, url: yield(:path)) do |f| %> <%= render 'shared/error_messages', object: @user %> <%= 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 %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit yield(:button_text), class: "btn btn-primary" %> <% end %> 

My registration view app/views/users/new.html.erb with partial:

 <% provide(:title, 'Sign up') %> <% provide(:button_text, 'Create my account') %> <% provide(:path, signup_path) %> <h1>Sign Up</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= render 'form' %> </div> </div> 

and my editing app/views/users/edit.html.erb :

 <% provide(:title, "Edit user") %> <% provide(:button_text, "Save changes") %> <% provide(:url, user_path) %> <h1>Update your profile</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= render 'form' %> <div class="gravatar_edit"> <%= gravatar_for @user %> <a href="http://gravatar.com/emails" target="_blank" rel="noopener noreferrer">Change</a> </div> </div> </div> 

My problem is that I do not know which correct path for the editing view I need to set using the provide method.

With signup_path, it works great for representing registrations. What I tried for editing looks like this:

  • user_path (currently in the sample code)
  • edit_user_path
  • edit_user_path(@user) (to pass a custom object)

Below is the current routing error I received:

enter image description here

and below that available user-related routes:

enter image description here

For example, if I do not use provide an edit view to provide a URL, it works fine, as here:

 <%= form_for(@user) do |f| %> <%= render 'shared/error_messages', object: @user %> <%= 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 %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit yield(:button_text), class: "btn btn-primary" %> <% end %> 

But the goal is to refactor the code using partial ones, so I need to provide a URL to represent the registration.

routes.rb :

 Rails.application.routes.draw do get 'sessions/new' get 'users/new' root 'static_pages#home' get '/help', to: 'static_pages#help' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/signup', to: 'users#new' post '/signup', to: 'users#create' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' resources :users end 
+5
source share
2 answers

So in the end, in order to pass the required URL as a requirement of the second exercise here , it works with providing only user_path . As I mentioned in my question, I already tried this with user_path , but what I did not see was what I used in the form of partial yield(:path) to insert the URL, so when you look to my question for the registration view, I provided the right variable with <% provide(:path, signup_path) %> , but in the edit view I used <% provide(:url, user_path) %> . Therefore, it is clear that I cannot provide the correct path from the editing view, even if I set the correct one, as you can see, as I described in my attempts.

So, for those who are also stuck in this exercise, after you tried it yourself and after that you looked exactly if you assigned your variables correctly . This is my working solution:

Partial form app/views/users/_form.html.erb :

 <%= form_for(@user, url: yield(:path)) do |f| %> <%= render 'shared/error_messages', object: @user %> <%= 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 %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit yield(:button_text), class: "btn btn-primary" %> <% end %> 

Registration view app/views/users/new.html.erb :

 <% provide(:title, 'Sign up') %> <% provide(:button_text, 'Create my account') %> <% provide(:path, signup_path) %> <h1>Sign Up</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= render 'form' %> </div> </div> 

Edit view app/views/users/edit.html.erb :

 <% provide(:title, "Edit user") %> <% provide(:button_text, "Save changes") %> <% provide(:path, user_path) %> <h1>Update your profile</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= render 'form' %> <div class="gravatar_edit"> <%= gravatar_for @user %> <a href="http://gravatar.com/emails" target="_blank" rel="noopener noreferrer">Change</a> </div> </div> </div> 
+2
source

You do not need to declare a path. If you are using resources :users . Your application will know how to set the path. If you declare:

 @user = User.new 

the path will be:

 new_user_path 

but if you declare:

 @user = User.find(params[:id]) 

the path will be:

 user_path(id) 

http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html

0
source

All Articles