I'm making a little form and what I'm trying to do is to make one of the text fields get the value only when notification is created. I have:
<%= f.label :User_ID %>
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
My problem is that when other users are editing notifications, then this text field is automatically changing it's value. I'm not sure how to make it work. Thanks!
In your view you could check if the user id is already present and if so, don't display the user id text_field:
<% unless user_id.exists? %> #only if user_id is not present, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
Another option would be to add a boolean value to the model you are referring. For example, a notification model:
notification: string
edit: boolean (default: true)
After a user creates a notification, you set the boolean value for "edit" to false with an after create for example.
The next time a user edits that notification, you do the same in the view as before:
<% unless #notification.edit? %> #only if edit is set to true, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
It is a little vague but it gives you an idea how to do it.
Related
I am working on ruby & rails.
My user model is stored with firstname, and lastname. Right now, I want to search from a collection of uses, and the search is in a form using form_for. I would like the drop down string show the user's full name and also ordered by user full name. How to do with that?
currently, I only know how to order it by last name, and show last name.
<%= form_for #user do |f| %>
<%= f.label 'First Choice'%>
<%= f.collection_select :first, User.order(:lastname), :lastname, :lastname, include_blank: true %>
<%end%>
write a full_name instance method to user model to access it with object.
class User < ActiveRecord::Base
def full_name
first_name.to_s + last_name.to_s
end
end
Then you can use the collection select like below -
<%= f.collection_select :first, User.order(first_name: :asc, last_name: :asc), :id, :full_name, , include_blank: true %>
I have an app where Question model has_many relationship with Option. I also have a button to add options while creating a question. Every question has only one correct answer. So when I create a question and click on Add Option button, new option is created but the new radio button associated with it has different name. In fact the name of radio button is of the form question[options_attributes][i][is_answer] where i is id. As far as I know radio buttons should have the same name to work as a collection or group. So how can I make it work as a group even if I create any number of options for a single question?
html.erb
<%= form_for #question do |form| %>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<%= form.fields_for :options, question.options.each do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= a.radio_button :is_answer, "options" %>
<%= a.check_box :_destroy %>
<%= a.label :_destroy, 'delete' %>
</div>
<% end %>
<%= form.submit 'Add option', :name => "add_option" %>
<%= form.submit 'Delete options', :name => "remove_option" %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
controller.rb
class QuestionsController < ApplicationController
def new
#question = Question.new
#question.options.build
end
def create
#question = Question.new(question_params)
#question.user = current_user
if params[:add_option]
#question.options.build
else
respond_to do |format|
if #question.save
format.html { redirect_to #question, notice: 'Question was successfully created.' and return }
format.json { render :show, status: :created, location: #question }
else
format.html { render :new }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
render :action => 'new'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question
#question = Question.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params.require(:question).permit(:body, options_attributes: [:id, :body, :question_id, :created_at, :updated_at, :is_answer])
end
end
There are two options:
Using JavaScript on the client-side to uncheck the radio buttons.
Using radio buttons with the same name. It this case you will have to change the way you pass the :is_answer parameter and manually assign the value in options_attributes.
Method 1 details:
See this question radio different names - only check one
Method 2 details:
Instead of passing :is_answer parameter for each option you can pass a single parameter for the question having chosen answer id as the value. Lets name it "answer_id". We want this parameter to be in the params[question]
hash in the controller, so the whole name will be "question[answer_id]". Although radio buttons are generated for each option, only the chosen one will be sent to the server as they all have the same name.
<%= form.fields_for :options, question.options.each do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= radio_button_tag "question[answer_id]", a.object.id, a.object.is_answer? %>
<%= a.check_box :_destroy %>
<%= a.label :_destroy, 'delete' %>
</div>
<% end %>
https://apidock.com/rails/v4.2.7/ActionView/Helpers/FormTagHelper/radio_button_tag
In the controller you will have to manually assign the option's is_answer parameter based on the answer_id value.
def question_params
result = params.require(:question).permit(:body, :answer_id, options_attributes: [:id, :body, :question_id])
answer_id = result.delete(:answer_id)
result[:options_attributes].values.each do |option_attrs|
option_attrs[:is_answer] = option_attrs[:question_id] == answer_id
end
result
end
If you need further details please let me know. I will update the answer to give more information.
I'm trying to add country selection in Devise registration and I use country_select gem from https://github.com/stefanpenner/country_select#example
There explains the simple usage by using country_select("user", "country") use model and attribute as parameters:
Problem: When I push submit button user was created and Everything is good except country column doesn't has data from my selection
Purpose: After submit registration I want to insert country which I have selected from Signup form into database(table: users, column: country) also
sign_up.html.erb
<h2><center>Sign up</center></h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs" style="float; margin:0 auto;width:35%">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true %>
<%= f.input :password_confirmation, required: true %>
<%= f.label :country %>
<%= country_select("user", "country") %> <<-- My model's name is user.rb and in my users table has a country column
</div>
<div class="form-actions" style="float; margin:0 auto;width:10%">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
**My model's name is user.rb and in my users table has a country column
Thanks for advance
try this out
<%= f.input :country, as: :country %>
I have solved my problem by following this Question and adapting to my issue Adding extra registration fields with Devise
I'm creating override controller(Registrations Controller in this case) for allow Devise to adding country variable to database
Create new registrations_controller.rb
I have added :country in this file
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters, :only => [:create]
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :country) }
end
end
You can see the original registrations_controller.rb at this link: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb
Create a route to allow Rails can routing to override controller by add these lines
in routes.rb
devise_for :users, :controllers => { :registrations => 'registrations' }
Important Please make sure that you don't have any devise_for :users line in your routes.rb, If you have, delete it
Well, now I can use <%= country_select("user", "country") %> to save country selection into database without any problem
I just give up. I have remembered password and login for my phpmyadmin on www.xxx.com/phpmyadmin like:
root
asdfasfsdfs
Then on my aplication on www.xxx.com/model/new , I have
<%= form_for #model do |f| %>
<%= f.text_field :city, :placeholder => 'Write Your city' %>
<%= f.password_field :password, :placeholder =>'SET PASSWORD' %>
<% end %>
Why browser put in text_field for city "root" and in password - password for root of phpmyadmin?
How can I remove autocomplete of this fields? I try:
autocomplete => 'off'
autofocus => false
:value => ''
$('#field').val('')
Nothings work... Peoples do not see placeholder text, because fields are filled ...
ANSWER:
Ok, I make by jQuery, like this:
$(window).load(function(){
$('.div_class input').val('')
});
So fields are filled but after load is done jQuery clear them.
just change the syntax a little. This worked for me :autocomplete => :off
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => :off %>
You can turn off the feature by setting the autocomplete attribute to off in the form or field.
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => 'off' %>
However, keep in mind that the directive is not compatible with all browsers and major browsers are moving towards ignoring the attribute for password fields, (e.g Firefox.)
NOTE: DON'T FORGET TO RESTART THE SERVER AFTER THIS CHANGE
<%= f.text_field :city, :placeholder => 'Write Your city', :autocomplete => 'off' %>
I'm trying to set the default value of a select tag to the value that is saved in the model in active record. At the same time, if no value is specified (e.g. on model-creation) I want a list to be displayed to select a certain field of the select tag.
This is how I'd like to do it:
<% valid_years = Array.new %>
<% for i in 1900..Time.now.year %>
<% valid_years << i %>
<% end %>
<%= f.label t :label_year_of_birth %>
<%= f.select :year_of_birth, options_for_select(valid_years), class: "" %>
However, the value that's saved in active record is not displayed in this select tag. Instead, the first value of the array is shown. Also, if I write options_for_select(valid_years, 1950), rails won't put the value to 1950.
However, if I use a simple text field tag, rails WILL put the saved value in it.
<%= f.label t :label_year_of_birth %>
<%= f.text_field :year_of_birth, class: "" %>
Replace
<%= f.select :year_of_birth, options_for_select(valid_years), class: "" %>
With
<%= f.select :year_of_birth, options_for_select(valid_years, form_for_object_name.year_of_birth), class: "" %>
where
form_for_object_name is the object that you are passing to your form since you didn't share the <%= form_for(?) do |f| %>.
options_for_select takes the second argument i.e. form_for_object_name.year_of_birth as the selected value.
If form_for_object_name.year_of_birth is nil then select will default to the first value of your options i.e. 1900 in your case.