I made a drop down list using collection_select
<%= collection_select(:page, :user_id, #users, :id, :full_name) %>
that part works fine. I am having trouble on saving it/processing it using the controller, the APIdock isn't very helpful on that part and I haven't been able to get the one example I found to work. Can anyone explain to me how I can process the selected value in the controller?
You will have a value
params[:page][:user_id]
which will correspond to the value selected in the form. You can see it inspecting the params variable.
IT is a number, the ID of the selected user. You could load the user by
#user = User.find(params[:page][:user_id])
but it's useless. In fact, if the user_id property of the page is accessible, then with the usual
#page.update_attributes(params[:page]) # in the update action
or
#page.create(params[:page]) # in the create action
you will get the user in the page as #page.user.
To store page values in model which should specify 'has_many :pages' in user.rb.
#user = User.find(params[:user_id])
#user.pages = params[:page]
params[:page] returns an array of values which will be store in current model record.
Related
I've got a Rails 4 app with a service that loads required objects on a new/edit document action, and this is a method from it:
def template_variables
if #document.template_variables.any?
TemplateVariable.where(id: document_vars).each do |v|
next unless User.method_defined?(v.name.to_sym)
v.update_attribute(:text, #user.send(v.name.to_sym)) # problem line, persists the change when i don't want to
v.text = #user.send(v.name.to_sym) # also a problem, doesn't update the value in the form at all
end
else
TemplateVariable.where(id: master_vars)
end
end
I need a solution for the two problem lines (they are just two things I've thought of, and they aren't supposed to both be there but I've included both for the sake of my problem).
The first updates and persists the change to the model which is behaviour I don't want. The second line doesn't do anything, where logically it seems like it should replace whatever text was in that variable in the form with #user.send(v.name.to_sym). It appears to do nothing.
Is there are solution to this problem that I'm unaware of?
Bonus points if there's a way to list the fields with new values to display in a flash[:notice].
Update now with relevant form code.
<%= v.input :text, as: :string, input_html: { value: v.object.text } %>
Setting the value/vs not setting it doesn't change anything either.
If you've got an instance variable that you're passing from your controller, you can set (but not save) values on that variable which will be available in the view.
For example, you can set the first and last name of a new user by passing in arguments or setting the attributes in the controller:
#UsersController
def new
#user = User.new(first_name: 'John')
#user.last_name = 'Smith'
end
In the view
#users/new.erb.html
<%= #user.first_name # will be John %>
<%= #user.last_name # will be Smith %>
But in your case, you're using update_attribute, which saves the record changes. Instead, you should be creating an instance variable, setting (but not saving) the values on that, and using that in the view/form
e.g.
#varibales = TemplateVariable.where(id: document_vars)
#variables.each do |variable|
#change what you want here
variable.foo = 'bar'
end
And then reference your #variables object in the view/form.
In saying all of that, you should strive to only use one instance variable in your controller, consider using form objects, if you need to pass multiple values from the controller to the view.
As for the flash notice, you can display whatever you like as a flash notice by setting that in the controller, assuming you've got your view setup to display flash notices as shown here
flash[:notice] = "The value of #{#variables.first.foo}"
I want for every users to have only one row of data in a table named business. He can edit it for the second time.
Also i want to submit each column value separately using form_for but when i do like this when submitting the form for the second time for a different column entry, it goes to the second row making the previous row empty.
How can i achieve this?
Here is my code...
<%= form_for #new_business do |f| %>
<%= f.text_area :first_problem %>
<%= f.submit %>
<% end %>
A screenshot of the data table
Table data screenshot
Any help is appreciated as i am new to rails. Thanks.
One user can only have one business, you need to set relationship between them
In user.rb model
has_one :business
In business.rb model
belongs_to :user
For your second problem, i think you are not passing row id for which you want to update columns, so everytime it is creating a new entry for that column
It looks like your code snippet is from your new template (i.e. "app/views/businesses/new.html.erb"). This should only be used when you want a new object created. Your controller probably says something like
def new
#new_business = Business.new
end
When the form is submitted, the :create action in your controller gets called, which should create a new record in the database.
When you want to edit that object, you use an edit action in your controller that corresponds to an edit.html.erb view. Something like this in the controller:
def edit
#business = Business.find(params[:id])
end
with a similar form to the one you listed above in the view. When that form is submitted, it should route to an :update action in your controller, that updates the existing record in the database.
The tricky part about what you are trying to accomplish is limiting each user to only being able to create one record in the database. There are many ways to go about doing this, but the general idea would be to restrict the user's access to the new and create actions in the controller once they have already created a record. You can do that by using before_action (if using Rails 4.0 or greater) or before_filter (if using Rails < 4.0) to call a method that checks if a user has already created a record.
I am totally new to Ruby on Rails, I was following some tutorials and developed a small CRUD application. In a form I have a drop down which was previously filled with hard coded values but now I have populated it with the values retrieved from database which works fine, but the trouble is I intend to get the selected value of the dropdown but instead of that I end up getting the id of that value, I know I am doing something logically wrong, I have did some research but was unable to come across a solution that fulfilled my requirement.
Here is my controller action,
def new
#list=CategoryType.all
end
Here is the dropdown within the form:
<%= f.label :maincategory %>
<% options = options_from_collection_for_select(#list, 'id','maincategory') %>
<%= f.select :maincategory, options %>
And this is the action getting back the parameters from the form in the controller:
private
def category_params
params.require(:category).permit(:name, :maincategory)
end
And this action saves it in the database:
def create
#category= Category.new(category_params)
if #category.save
redirect_to #category
else
render 'new'
end
end
Instead of maincategory id is being saved in the database, i am totally new to ROR, would really appreciate the help. Thanks in advance.
Why would you save the maincategory as a string ?
I suppose in your Category model, you should have a belongs_to :category_type.
I think it's better to save the CategoryType id of your maincategory object.
This way, you have the correct relation between your category and its type. Therefore, you could have the CategoryType of your category with this command : #category.category_type.name, if you want to retrieve the name of your record, as your data relation would be correct.
Let me know if I misunderstood something !
Problem solved. HTML5 localStorage messed with me.
I'm trying to populate a form with parameters from the new()-method, and I can't get it to work.
Every user has default values for the form saved in the database(in a table called defaults), and when you create a new record I want it to be populated with the values from that table.
#default = Default.find_by_user_id(current_user.id)
#invoice = Invoice.new(:title => #default.title, :company_information => #default.company_information)
render 'create'
and then in my view:
form_for #invoice, :url => { :action => "create"} do |f| ...
What happens is that the values that are default for invoice are created, but not the ones created in the new()-method.
The weirdest part is that when I check the source code after the page is loaded, the inputs value attributes is filled with the correct information, but not rendered on the page...
What you're doing here:
Invoice.new(:title => #default.title, :company_information => #default.company_information)
Makes sense and should work…unless those fields are protected from mass assignment.
class Invoice << ActiveRecord::Base
attr_accessible :some, :other, :fields
...
end
This would allow you to set :some, :other, (and) :fields when you initialize your Invoice object, but it will prevent you from setting any other "attributes".
Strange, I don't see anything wrong with what you are trying to do... maybe something on the browser side (javascript, css, etc) is fowling things up?
Check to see if there is something selectable inside the form inputs or try creating a vanilla form without any javascript or css. Or, you might even try simply printing the contents of the attribute in the html (without using input/textarea tags) using something like:
<%= #invoice.title %>
This will at least help confirm that the default values where indeed set. Additionally, using:
<%= f.object.title %> # place me inside the form_for block
will help you confirm that the form builder instance also has the correct value.
Good luck.
I would like to know which way is the best to resolve my question :
I have a form in order to select people via a select field. If the name is missing in the select field, a text field is available to add directly the person's name.
- The form in new.html.erb is the format of the new action of the Team controller.
- The list of the people is extracted from the People model.
def new
#team = Team.new
#people = People.all
end
I created an attribute in the Team model to store the new_person text field :
class Team < ActiveRecord::Base
attr_accessor :new_person
...
end
Finally, here's an extract of my view :
<%= f.select :person_id, #people.map { |p| [p.name, p.id] } %>
<%= f.text_field :new_person %>
Obviously, I would like to save the new person in the table Person before saving the data from the form. As usual, the id are saved instead of the names
At this point, I've got two issues :
1/ The params array has the key new_person what doesn't have the table. So it is not possible to use the Team.new(params[:team]) method. Does exist an easy solution to avoid this problem ?
2/ As I need the person_id, how can I get it when the name comes from the new_person field? In using the before_filter method ?
Thanks a lot,
Camille.
1) You should consider using fields_for in your view within your form_for block. This will allow you to specify that the fields within the fields_for block are attributes of a different model, will generate the appropriately named input fields, and allow you to use params[:team] in your controller. See the FormHelper documentation for more on this.
2) While you could do something in your controller to first check for a value in the new_person field, create the record, update the contents of params[:team] with the value of the newly created person and create the team, this feels a bit like a hack to me. Another possible solution which may be less fragile would be to use some JavaScript on the page that would render some kind of modal dialog for the user to create the new person, submit the new person to the person#create controller method, then refresh your drop down. It would probably not be terribly difficult to do this using a jQuery UI modal form (very good example at that link to do what you need) with Rails remote form and unobtrusive JavaScript.
This is probably a more difficult solution to your second question than you are hoping for, but probably more useful in the long run.