i have model users, type_users and details_users. an users may have many type_users and type_users may have many users. user's form is:
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="field">
<% for type_user in TypeUser.all %>
<div>
<%= check_box_tag "typeuser[typeuser_ids][]", typeuser.id %>
<%= typeuser.name %>
</div>
<% end %>
</div>
my question is:
how can i save the user(table users) and the types of users(table type_users) selected, saving the user in one table(table users), and the type of user(table details_users) on another table ?
I supose you have user_id and typeuser_id fields in your DetailsUser table so
In the user controller in action create after "if #user.save"do this
if #user.save
typeusers = params[:typeuser][:typeuser_ids]
length = typeusers.length
length.times.with_index do |i|
DetailsUser.create(:user_id => #user.id, :typeuser_id => typeusers[i])
end
Related
I have a simple form as follows
<% provide(:title, 'View Mail') %>
<h1>View Mail</h1>
<div class="row">
<%= form_tag('/mails/new') %>
<%= label_tag "Email Address" %><%= email_field_tag(:email) %><br>
<%= label_tag "Password" %><%= password_field_tag(:password) %><br>
<%= submit_tag "View my Mails" %>
</div>
<% end %>
What I actually want to do is capture the email and password field and forward them to another controller 'mails' so that I can use the value of email and password in that controller and then show the appropriate details.This is just a sample app for me to check something as I am new to rails.
What exactly should be in place of
<%= form_tag('/mails/new') %>
<% provide(:title, 'View Mail') %>
<h1>View Mail</h1>
<div class="row">
<%= form_tag('/mails/new') do %>
<%= label :email %>
<%= text_field :email) %><br>
<%= label :password %>
<%= password_field :password %><br>
<%= submit_tag "View my Mails" %>
<% end %>
</div>
<% end %>
You need to define 'mails/new' path, in config/routes.db to be able to access that path.
Hopefully it answers your problem.
I recently followed a tutorial about nested forms by Ryan Bates and did basically the same thing as he did just with other names. I wanted to nest assignments in the order form and I would like to build an assignment for every bun to that order and the user should put a count in the form.
So my controller looks like this
def new
#order = Order.new
#buns = Bun.all
#buns.each do |bun|
#order.assignments.build(:bun_id => bun.id)
end
end
And the _form partial looks like this
<%= form_for(#order) do |f| %>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div id="assignments" class="field">
<% f.fields_for :assignments do |builder| %>
<div id="assignment" class="field">
<%= builder.label :count, "Anzahl" %>
<%= builder.text_field :count %>
<%= builder.object.bun_id %>
<% end %>
</div>
</div>
<div class="field">
<%= f.label :due_to %><br />
<%= f.datetime_select :due_to %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The Order model has this part
has_many :assignments
has_one :user
accepts_nested_attributes_for :assignments
And the Assignments model this one:
attr_accessible :bun_id, :order_id, :count
belongs_to :bun
belongs_to :order
As i log the assignments out there are all, which should be build, so why weren't the fields rendered?
Thanks for your help!
You are missing an equal sign (=) in your erb :
<%= f.fields_for :assignments do |builder| %>
right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)
I am a newbie in ROR, so please help me with this.
I created a model named 'Customer' from rails console, in the database there is a table named 'Customers' and it has columns: id, name, address, gender, dob, credit_card, created_at and updated_at. So, to save these information I created a view such as:
<%= form_for(#customer) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :dob %><br />
<%= f.date_select :dob %>
</div>
<div class="field">
<%= f.label :gender %><br />
<%= f.text_field :gender %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :phone_number %><br />
<%= f.text_field :phone_number %>
</div>
<div class="field">
<%= f.label :credit_card %><br />
<%= f.text_field :credit_card %>
</div>
<div class="actions">
<%= f.submit 'Order', :action => :save_order %>
</div>
this view would be called by 'public' controller, from action 'check_out'
the code for action check_out is as:
def check_out
#customer = Customer.new
end
and code for action 'save_order' would be:
def save_order
#customer = Customer.new(params[:customer])
if #customer.save
#somcode
end
end
but i am getting a problem in the view part, "undefined method `customers_path' for #<#:0x9f49814>" in the line which contains "form_for(#customer)"
what i am doing wrong ? I am using rails 3.2.2
Thanks in advance :)
You don't have route for customer controller actions. Just add this line to routes.rb file
resources :customers
This one will generate several helpers, one of them will be customers_path. You can list all path helpers by command
rake routes
I have a two models, Group and User
User belongs_to Group and
Group has_many Users
In my groups/show.html.erb I have the user sign-up form
<h1>Create user</h1>
<%= form_for #user do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<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, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
To get a relation between the Group and the User I have a create method in my Users controller as follows
def create
#group = Group.find(params[:group][:id])
#user = #group.users.build(params[:user])
if #user.save
flash[:success] = "You have created a new user"
redirect_to group_path
else
#title = "Create user"
render 'new'
end
end
I have also tried:
#group = Group.find(params[:id])
and
#group = Group.find(params[:group_id])
But I still get an error
Essentially I want to create new users in the group/show.html.erb and associate that user with the group where the user was created. If the user is created in groups/3 for example, how do I set my create method in the Users controller to make sure this relation holds?
In general I've been following the Hartl Rails tutorial book at http://ruby.railstutorial.org/chapters/user-microposts#sec:creating_microposts and following the approach for forms and create methods. However I am not sure how to get the params for groups/3 into the find method like #group = Group.find(?????)
Can someone please enlighten me, this issue has been bothering me for a few days now.
Thanks in advance
After the form is submitted, it takes you to the users#create. This route doesn't have a group_id segment.
To pass group_id there, you need to store it in a hidden field in your form.