RoR: how can I get a micropost to be created by two seperate micropost forms according to an element? - ruby-on-rails-3

My microposts have a column called type which is a string. This can either be purchase or sale. I want two seperate input forms, where if you input content into one then it automatically fills in purchase as the type (when creating the micropost) and if you input content into the other it automatically fills in sale. .
Here is my form as it is
<%= 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?" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
I recently added type as a column in the micropost data table. That is why there is no input for it (yet). Read above for how I want the type to be automatically filled in
Im thinking one way to do it is to id each form with something. Then when the form is being filled in, I could somehow tell it to automatically fill in the hidden type field based on which form is filled in. IS this possible??

I think you are referring to the hidden_field here. Use hidden_field to pass information to the controller without presenting it to the user. Here is one form for the purchase:
<%= 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 :type, 'purchase' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and another for the sale:
<%= 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 sell?" %>
<%= f.hidden_field :type, 'sale' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

Related

Pass values of a submitted form to another controller in rails3

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.

RoR: why aren't my microposts showing up?

Here is the users show view where they are supposed to show up. ..
edit (I have updated this post slightly you can see it at RoR: How can I get my microposts to show up?)
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<div id="purchases list">
<ol class="microposts">
<%= render #purchases unless #purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render #sales unless #sales.nil? %>
</ol>
</div>
so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase)
Here are the forms:
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
also, here is the show part of the users_controller.rb
def show
#user = User.find(params[:id])
#micropost=Micropost.new
#microposts = #user.microposts.paginate(page: params[:page])
end
and here is the show part of the microposts_controller.rb
def show
#micropost = Micropost.find(params[:id])
#microposts = Micropost.where(:user_id => #user.id)
#purchases = #microposts.where(:kind => "purchase")
#sales = #microposts.where(:kind => "sale")
end
can anyone help me out? anything else i need? hmmm
First, just to be sure you are getting the results you want, you should try something like this in your view
<%= #sales %>
This should be a hash of the results you want. Then, if that looks good, you want to do something like this
<div id="sales_list">
<ol class="microposts">
<% if #sales.any? %>
<% #sales.each do |sale| %>
<li><%= sale %></li>
<% end %>
<% end %>
</ol>
</div>
And repeat for purchases

RoR: how can I list only microposts that have a certain attribute in one column?

Right now I am listing all of a users microposts using the code below.
<div class="span8">
<% if #user.microposts.any? %>
<h3>Purchases I am interested in (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
and the view that renders for _micropost.html.erb is as follows
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
so this works fine, however I am changing things up. Every micropost has a hidden_tag_field which is a string (and a column in the database) that is called kind. It can be either "purchase" or "sale". I want to list all of the purchase microposts in one place and all the sale microposts in another. How can I change the micropost view to do this?
Controller:
#purchases = #microposts.where(:kind => 'purchase')
#sales = #microposts.where(:kind => 'sale')
View:
<h2>Purchases</h2>
<%= render #purchases %>
<h2>Sales</h2>
<%= render #sales %>

RoR: why am I getting undefined method 'hidden_field_tag'?

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 :)

rails 3, checkbox association table

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