AutoPopulate Form Values on Edit With Rails - ruby-on-rails

I have a form that I copied over from new.html.erb and put it in edit.html.erb. I essentially want the same form, but if there are values already in the database for the form fields I want them to be pulled into the form for editing. I currently have something like this:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
.
.
.
.
<div class="actions"><%= f.submit %></div>
<% end %>
How can I pull from the the database to fill in the fields with appropriate values?

This will happen automatically if you set #user in the 'edit' action of your controller - something like
def edit
#user = User.find(params[:id])
end
you might also think about including the form as a partial rather than repeating it to DRY up your code - this link gives the general idea

Related

Check the URL path using ruby on rails

In my project, the gems that I've used are: rails admin, cancancan and devise.When I do the bundle install, lots of views will be added. I added "can :crud, [xxxx, xxxxx]" to have a CRUD in my ability model.
Question: How do I check the current path in my view? because I want to disable some field. Example: I am in the new path method and have 3 fields for that(then I clicked the submit/create, now it will the added data). I clicked the edit button,then there's one field will be disabled.
Note: The both form of new path and edit path is in the same file(activities_types/_form.html.erb).
View
_Form.html.erb
<% if current_page?(new_activity_type_path) %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<% else %>
<div class="field">
<%= f.label :"name" %>
<%= f.text_field :name, disabled: true %>
</div>
<% end %>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
if you want to diffenetiate between edit and new actions in view then
<% if params[:action] == "new" %>
...
<% elsif params[:action] == "edit" %>
...
<% end %>
OR
<div class="field">
<%= f.label :name %>
<% if params[:action] == "new" %>
<%= f.text_field :name %>
<% else %>
<%= f.text_field :name, disabled: true %>
<% end %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
I would set up an instance variable in your new and edit methods in your controller, following that up by checking the value in your form.
example:
def new
#whereAmI = "new"
end
def edit
#whereAmI = "edit"
end
In the view:
<% if #whereAmI == "edit" %>
<%= f.text_field :name, disabled: true %>
<% elsif #whereAmI == "new" %>
<%= f.text_field :name %>
<% end %>
You can check the action like this:
if params[:action] == "new"
# doing something
elsif params[:action] == "edit"
# doing something
end

Weird redirection when updating datas

I generated a scaffold for a portfolio via the Rails command, rails g portfolio titre:string, thumbnail:string lien:string description:text. I also added FriendlyId to get a better URL, and that's about all. Here is the 'update' action.
def update
if #portfolio.update(portfolio_params)
redirect_to #portfolio, notice: 'Portfolio mis à jour.'
else
render :edit
end
end
However, when trying to update a project in my portfolio, the submit button tries to get to 'portfolio#update' via patch, but puts a '.' instead of a '/' which gives me No route matches [PATCH] "/portfolios.test-1"
For the route, I only have resources :portfolios
edit : added the form
<%= form_for #portfolio, url: portfolios_path(#portfolio) do |f| %>
<% if portfolio.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(portfolio.errors.count, "error") %> prohibited this portfolio from being saved:</h2>
<ul>
<% portfolio.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :titre %>
<%= f.text_field :titre %>
</div>
<div class="field">
<%= f.label :categorie %>
<%= f.text_field :categorie %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :thumbnail %>
<%= f.file_field :thumbnail %>
</div>
<div class="field">
<%= f.label :lien %>
<%= f.text_field :lien %>
</div>
<div class="actions">
<%= f.submit 'Enregistrer' %>
</div>
<% end %>
I didn't have any other possibilities that doing #portfolio, url: portfolios_path(#portfolio), otherwise Rails considered that '#portfolio' was nil
edit 2 : added the private 'set_portfolio' params
private
# Use callbacks to share common setup or constraints between actions.
def set_portfolio
#portfolio = Portfolio.friendly.find(params[:id])
end
What's wrong with my app ?
portfolios_path is the collection path.
change it portfolio_path should fix it.
<%= form_for #portfolio, url: portfolio_path(#portfolio) do |f| %>
I guess you are going to use this form for creating new portfolio as well, so change it to <%= form_for(#portfolio) %> should fix it and will also work for both cases. form_for will submit to correct path.

Rails - Show updated fields when editing

I am rolling my own authentication, and the issue I am running into is the edit form for my users. I here is the form...
#app/views/users/edit.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', root_path %>
#app/views/users/_form.html.erb
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']) %><br/>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
The issue I am having is that when I display the edit form the :user_type field is not correct. I want this field to reflect what is currently saved for the current user, instead I just get the drop down with the first option in the list displayed.
You want to either use:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type) %>
or:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']), :selected => #user.user_type %>
options_for_select has second parameter to preselect it...
options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type)

Setting a value sent from a link_to a partial in another view

So i have this link from a partial in a view:
<td><%= link_to 'Inscribir Ejemplares en la Carrera', home_carreras_path(#race, :meeting_id => meeting.id) %></td>
That link sends me to another view where two other partials are rendered.
The browser address looks like this:
http://0.0.0.0:3000/home/carreras?meeting_id=6
So you see that the value i sent from the link is there, and i want to pass that value, that number to one of the partials rendered in that view.
i haven't found how to do that, i searched a lot before asking. So please i hope someone here can help me. Thank you.
EDIT: ADDING MORE INFO
Controller:
class HomeController < ApplicationController
def index
render :layout => 'principal'
end
def carreras
#meeting_id = params[:meeting_id]
render :layout => 'principal'
end
end
Not much action there :/
The Partial where the hidden field is:
<%= form_for(race) do |f| %>
<ul>
<% race.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :meeting_id %><br />
</div>
<div class="field">
<%= f.label :carrera_num %><br />
<%= f.text_field :carrera_num %>
</div>
<div class="field">
<%= f.label :caballo_num %><br />
<%= f.text_field :caballo_num %>
</div>
<div class="field">
<%= f.label :caballo_nombre %><br />
<%= f.text_field :caballo_nombre %>
</div>
<div class="field">
<%= f.label :distancia %><br />
<%= f.text_field :distancia %>
</div>
<BR>
<div class="actions">
<%= f.submit "Inscribir Ejemplar" %>
</div>
<% end %>
If your controller actions for carreras, you can set an instance variable for the meeting_id.
def carreras
#meeting_id = params[:meeting_id]
# rest of controller action
end
#meeting_id will then be available to you throughout your rendered action.
You can supply your hidden_field tag with an options hash to set the value.
<%= hidden_field :meeting_id, value: #meeting_id %>

undefined method for form fields

I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.

Resources