I have a form with some fields in my rails 2 application and I want to prepopulate the value from what was entered before. The parameters values are in the url
here is the code for the form:
<% form_tag({:controller => "articles", :action => "search"}, :method => "get") do %>
<%= label_tag("start_date") %>
<%= text_field_tag("start_date","", :type => "date") %>
<%= text_field_tag "username", "", :placeholder => "username" %>
<%= text_field_tag "email", "", :type => "email" %>
<%= select_tag "status", options_for_select([["status", ""],"approved", "unchecked"])%>
<%= submit_tag("Search") %>
<% end %>
I have tried to use for instance #username = params[:username] in the controller but I still get an empty field
Problem is you're providing empty content for the field:
<%= text_field_tag "username", "", :placeholder => "username" %>
Rails doesn't automatically takes instance variables based on the field name, so if your controller defines a #username variable, you have to explicitly use it:
<%= text_field_tag "username", #username, :placeholder => "username" %>
I am not sure if it is working in Rails 2 but I would try to do something like this:
<%= text_field_tag "username", :placeholder => #user.name %>
Of course, in the controller action that renders this view you need to provide #user
Related
I am trying to create an Edit User Form to amend the user details stored in my database but keep getting error. I have changed the form URL and tried changing it to a method: :get without much success.
Edit User Form
<%= form_with url: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field "user[email]", placeholder: "email", :class => "form_box" %>
<%= form.text_field "user[username]", placeholder: "username", :class => "form_box"%>
<%= form.text_field "user[password]", placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
Edit Action
def edit
#user = User.find(params[:id])
end
Link to Edit User Form
<%= link_to "Edit", edit_user_path(#user) %>
Error Message
No route matches [POST] "/users/58"
You need to use model: #user not url: #user.
<%= form_with model: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field :email, placeholder: "email", :class => "form_box" %>
<%= form.text_field :username, placeholder: "username", :class => "form_box"%>
<%= form.password_field :password, placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
When you pass a model form_with will set both the action (/users or /users/:id) and method (POST or PATCH) depending on if the record has been saved. See Resource Routing: the Rails Default.
The url: option should really only be used if you have to override the conventional routes or if you have a form that does not wrap a model instance.
Also use form.text_field :email instead which will "bind" the input to the model attribute. Nobody likes having to refill an entire form from scratch because they missed some minor detail.
And on a side note do not use placeholders instead of labels - its an aweful practice from both a useability and accessibility standpoint.
I have a form_tag in form which contains two drop down boxes. When I submit the form both fields id isn't pass I dont know where did i do a wrong...
<%= form_tag '/daily_report' do -%>
<label>Search</label>
<%= select_tag "id", options_from_collection_for_select(User.all, "id", "firstname",:selected => #s) %>
<%= select_tag "id", options_from_collection_for_select(Project.all, "id", "name",:selected => #s) %>
<%= submit_tag "Search", :name => nil %>
<% end -%>
If you use the same name the values will be overwritten and you will get only one result. So change the name and make it different.
<%= select_tag "users", options_from_collection_for_select(User.all, "id", "firstname",:selected => #s) %>
<%= select_tag "projects", options_from_collection_for_select(Project.all, "id", "name",:selected => #s) %>
You can now access using the names, :users and :projects
I am building message app in rails where user can send message from templates. I have a database of templates, and the user can select templates as per category.
In my message model, I want to render the template dynamically based on category selected. I looked for examples on google, but was not able to find a relevant solution.
This is my message form:
<%= form_for #message, :html => {:multipart => true} do |m| %>
<%= m.select :biz_case, options_for_select(Message::Bcase), :prompt => "Select business case" %>
<%= m.text_field :subject, :class => "message-text", :placeholder => "Subject" %>
<div class="message-body">
<%= m.text_area :message, :class => "message-body", :class => "redactor", :placeholder => "Your content" %>
</div>
<%= m.select :user_type, options_for_select(Customer::CType), :prompt => "Customer segment" %>
<%= m.submit %>
<% end %>
In the above form, I am looking to display the subject and body based on the selected business case. Something like:
if biz_case == "promote"
subject = #template.subject where ("biz_case = ?", "promote")
message = #template.content where ("biz_case = ?", "promote")
end
The subject and message would be displayed in input text fields.
Can anyone tell me how to do this?
in your method:
#subject = #template.subject where ("biz_case = ?", "promote")
in view:
<%= m.text_field :subject, :value => #subject, :class => "message-text", :placeholder => "Subject" %>
I am a user model and in my view I have:
#user = current_user
User model have an attribute with name "email".
and I want send one e-mail to multiple email address with a subject.
I have a form like:
<%= form_for (Email.new), :method => :post, :remote => true, :url => { :controller => "users", :action => "invite_friends" } do |f| %>
<%= f.text_field :address1 %>
<%= f.text_field :address2 %>
<%= f.text_field :address3 %>
<%= f.text_field :address4 %>
<%= f.text_area :subject_email %>
<% end %>
Have I that create a "email model" with attributes address and subject_email?
Check section 2.3.3 Sending Email To Multiple Recipients from the Rails Guide
I have a web form to which I want to take a parameter from the URL and add it to the web form as a hidden field on the form.
<%= form_tag(:action => 'create') do |f| %>
<%= text_field_tag :email,"Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;", :size => "26" %>
<%= hidden_field_tag :ref_code, :id => 'ref_code', :name => 'ref_code', :value => params[:ref_code] %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
If I just do a:
<%= params[:ref_code] %>
I get the value I want which is a five character alphanumeric, however when I use it in the form, I get the full hash description:
{:id=>"ref_code", :name=>"ref_code", :value=>["k53e5", "home", "index2"]}
Why? I tried .values, .to_s, and other ways of getting by key and I always get the full hash instead of just the value. What am I doing wrong? Thanks.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
hidden_field_tag(name, value = nil, options = {})
<%= hidden_field_tag :ref_code, params[:ref_code], { :id => 'ref_code', :name => 'ref_code' } %>