I am trying to get my form to assign values to text fields. I have a helper file that defines current_user. I want the text_field :name to be the current_user.name.
<%= form_for #message, :url => contact_path do |form| %>
<%= form.text_field :name = current_user.name %>
<%= form.text_field :email = current_user.email %>
...
I have tried #{name}, params, how do you assign this value?
You should do that in your controller
for example: (See 3rd line)
def update
#message = Message.find(params[:id])
#message.name = current_user.name ## HERE
#message.update_attributes
end
Unless you mean you want it to be the default value when the page renders, in which case:
<%= form.text_field :name, value: current_user.name %>
or if you like
<%= form.text_field :name, placeholder: current_user.name %>
Add value option to the text_field helper as:
<%= form_for #message, :url => contact_path do |form| %>
<%= form.text_field :name, value: current_user.name %>
<%= form.text_field :email, value: current_user.email %>
...
Related
I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.
form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect
How would I go about converting this form tag below into a form_for?
<%= form_tag(contact_email_path, :method => 'post') do %>
<%= label_tag "Your email" %>
<%= text_field_tag "sender", #sender, :autofocus => true %>
<%= label_tag "Subject" %>
<%= text_field_tag "subject", #subject %>
<%= label_tag "Message" %>
<%= text_area_tag "message", #message %>
<%= submit_tag "Send Email" %>
<% end %>
form_for is a helper for creating forms which create or edit a resource.
If you have a resource here that you would like to create in your database, you would use this method. What it looks like you're doing here is not creating a resource, but sending an email. If that is the case, then a form_tag is probably a better option.
If you are, however, trying to create a new resource in the database (i.e. an new instance of ContactEmail or some other class), then you could do it like this:
<%= form_for #contact_email do |f| %>
<%= f.label :sender, "Your email" %>
<%= f.text_field :sender, :autofocus => true %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :message %>
<%= f.text_area :message %>
<%= f.submit "Send Email" %>
<% end %>
This assumes that #contact_email is an object that has the methods sender, subject and message and that you have resources :contact_email in your routes file.
I have this form:
<%= form_for(Imagedocu.new) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id, params[:id]%>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
How you can see i assign a value to one of the inputs:
<%= f.text_field :patient_id, params[:id]%>
I dont know why but now somehow i get the error:
undefined method `merge' for "73539":String
73539 is the params[:id]! What do i wrong?
This is a form that already contains an object (Imagedocu.new), so it "grabs" the value for patient_id directly from this object. If you want to overwrite this value, use this:
<%= f.text_field :patient_id, value: params[:id] %>
OR initialize the Imagedocu.new with a patient_id equal to params[:id]:
<%= form_for( Imagedocu.new(patient_id: params[:id]) ) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id %>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
But a cleaner way is to initialize the new Imagedocu object in the controller's action:
# controller
def new
#imagedocu = Imagedocu.new(patient_id: params[:id])
# etc.
# view
<%= form_for( #imagedocu ) do |f| %>
# etc.
Hello i have a form like
<%= form_for #user,
:url => url_for(:controller => 'frontend',
:action => 'registration_completion') do |f| %>
<div class="control-group">
<%= f.label :name, "Jméno", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
And user is displaying the form where he can see already registered values, but some of that value i want to hide if some condition (specifically i need hide value of username if it contains #) It is possible? thank you
solution: it seems that
<% if #user.name.include?('#') %>
<%= f.text_field :name %>
<% else %>
<%= f.text_field :name, :value => "" %>
<% end %>
works
I strongly suggest to put your logic inside a helper method:
module UserHelper
def name_input(instance, f, options = {})
options[:html_options] ||= {}
f.text_field :name, options[:html_options] if instance.name.include?('#')
end
end
Then in your view:
<%= name_input(#user, f, html_options: { class: 'text_field' }) %>
If you want to simply hide the value you can do:
<%= f.text_field :name, value: #user.name.include?('#') ? #user.name : '' %>
Feel free to move this to a helper method also.
To create a hidden field when the username contains an # symbol you could use the following helper method
module UserHelper
def hidden_name_field(user, f, options = {})
options[:html_options] ||= {}
if user.name.include?('#')
f.hidden_field(:name, options[:html_options])
else
f.text_field(:name, options[:html_options])
end
end
end
and use it in you form:
<%= hidden_name_field(#user, f, html_options: { class: 'text_field' }) %>
I'm making a simplistic message board with tags. The message#index view displays a list of all messages. The tag#show view shows messages of a specified tag. On the message#index view, there is a form (partial) that requires the user to write a message and to tag it. On the tag#show view, I'd like to use the same form partial but to have the view's tag automatically filled into the form. In the show action of the tags controller, the name of the tag is #title.
The form partial looks like this:
<% form_for :message, :url => { :action => "create" }, :html => { :id => 'form' } do |f| %>
<%= f.error_messages %>
<%= f.label :tag, "tag<p2>( separate tags with a comma )</p2>" %>
<%= f.text_field :tag_list %>
<%= f.label :name, "name<p2>( optional )</p2>" %>
<%= f.text_field :name %>
<%= f.label :email, "email<p2>( optional )</p2>" %>
<%= f.text_field :email %>
<%= f.label :title, "message" %>
<%= f.text_area :content %>
<%= f.submit 'submit' %>
<% end %>
How do I auto fill the tag_list text field with the #title value? #title is a string. I appreciate any help you can offer. Thank you.
<%= f.text_field :tag_list, :value => #title %>