I have Model1, which has "has_many" rel with Model2. What I need is to create form for Model2 under each object on Model1 index page. Lets say I have Tweets and each tweet has form to add comments. I have troubles in understanding how to do it, how to get Tweet id on index page and so on.
Seems like all you need to do to get #comment form working under each #tweet on index page is:
- #tweets.each do |tweet|
= tweet.name `#to show tweet`
- tweet.comments.each do |comment| `#to show each tweet comment`
= comment.content
= form_for [tweet, Comment.new] do |f| `#form forcomment belonging to this particular tweet`
= f.text_field :content
= f.submit
Hope it helps :)
Let's say tweets has many comments, so create model Tweet and Comment with relationship also create controllers tweets and comments.
In your tweets/index page there are list of tweets, right and script is like this.
<% #tweets.each do |tweet| %>
<%= link_to "show tweet", tweet_path(tweet) %>
<% end %>
show tweet link will take you to show page there you will get tweet id from params. Now you can make comment form in this show page as below
<%= form_for #comment do |f|%>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>
Routes file:
resources :tweets do
resources :comments
end
I have just provided you some way to work by giving examples of rails-views, so check action in your controllers properly you will do it.
Related
I'm having trouble paginating a topic's posts with Kaminari. The paginator partial shows up and seems to be calculating the number of pages it should have correctly, but all of the topic's posts are displayed on each page instead of the 2 posts I'm trying to get.
I have Topic and Post models, controllers, and views. A topic has_many posts, and a post belongs_to a topic.
I'm running Rails 4.
controllers/topics_controller
class TopicsController < ApplicationController
[snip]
def show
# Paginate
#topic = Topic.find(params[:id])
#posts = #topic.posts.page(params[:page]).per(2)
end
[snip]
end
views/topics/show.html.erb
[snip other renders for header, breadcrumbs, etc.]
<%= render 'panel', :topic => #topic %>
[snip other renders]
views/topics/_panel.erb
<%= paginate #posts%>
[snip containing divs]
<h2><%= link_to topic.title, category_path(topic.id) %></h2>
<%= topic.description %>
[snip containing divs]
<%= render 'posts/posts', :topic => topic %>
posts/_posts.erb
<% for post in #topic.posts %>
[snip table of post information]
<% end %>
I'm having trouble displaying my form at /users/2/friends/new. I'm receiving
undefined method `friends_path' for #<#<Class:0x21f0c14>:0x21ef364>
Here is the beginning of the form
<% form_for(#friend) do |f| %>
And the friends controller
def new
#user = User.find(params[:user_id])
#friend = #user.friends.build
end
This is the route
resources :users do
resources :friends
end
And the relevant path from "rake routes"
users/:user_id/friends/new(.:format) {:controller=>"friends", :action=>"new"}
Any help or insight is greatly appreciated. This is my first rails 3 app.
Try:
user_friends_path(#user)
It's because it's a nested resource:
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Update:
As for the form, you can do:
<%= form_for [#user, #friend] do |f| %>
I have about 3 models that all relate to each other.
Class Invoice < ActiveRecord::Base
belongs_to :shipperline, foreign_key: 'receiver_quantity'
def shippedqty
Shipperline.getshippqty(#customerid).to_a.split
end
Then i have a shipperline model
Class Shipperline < Activerecord::Base
has_many :invoices
def self.getshippqty(customerid)
Shipperline.joins(:customerorderline, :shipper).select("cust_order_id").where(cust_order_id: customerid).order("shipped_qty").pluck(:shipped_qty).uniq
end
However in my form (which is on my main view because this query will usually return 4 to 5 results per invoice which is why i need a user to select which one it really belongs to.
So i have been trying
<%= form_for :invoice do |f| %>
<td id="putcolor"><%= f.select :shippedqty, Shipperline.getshippqty(#customerid).to_a.split %> </td>
<% end %>
but it becomes empty. I have also tried collection_select and that dose not work either.
<td id="putcolor"><%= f.collection_select :invoice_id, #shipqty, :id, :shippedqty %></td>
but then i get undefined method 'map' for nil:NilClass
Can someone explain what im doing wrong or please point me in the right direction? I am fairly new to rails.
Also is there a better approach to trying to get a select box with these various results in it for the user to pick from?
Got this to work much easier this way.
My Invoice_helper.rb
def options_for_invoices(customerid)
Invoice.shippedqty2(customerid)
end
def options_for_packlistid(customerid, shipqty)
Shipperline.packlist(customerid, shipqty)
end
Then in my form i did:
<%= form_for(#invoice) do |f| %>
<%= f.label :shippqty %>
<%= f.select :shippedqty, options_for_invoices(#invoice.customerorderid) %>
<br />
<%= f.label(:packlistid %>
<%= f.select :packlistid, options_for_packlistid(#invoice.customerorderid, #invoice.shippedqty) %>
<%= f.submit %>
<% end %>
Now when it shows a drop down collection with the correct values and inserts the correct values into the database for each record.
I want to put a search form on my homepage at pages#home. I'd like to be able to search my model by category. I haven't been able to find a solution where you're actually putting the logic on a different controller than the models you're searching on. I was wondering if someone could help with the syntax and where it needs to go. Here are my relations:
vendor.rb
class Vendor < ActiveRecord::Base
validates :category_id, presence: true
belongs_to :category
end
category.rb
class Category < ActiveRecord::Base
has_one :vendor
end
pages_controller.rb
def home
#vendors = Vendor.all
end
routes.rb
root 'pages#home'
I'm trying to put the search form on home.html.erb, which is under the pages layouts. Was hoping someone could help with how I can accomplish this. This being (seemingly) a simple type of search, I'd hopefully not have to use a gem.
EDIT: ANSWER
For those searching, here's what worked. Thank you #Vla
vendors_controller.rb
def search
#vendors = Vendor.search(params)
end
pages/home.html.erb (this is my root 'pages#home')
<%= form_tag vendors_search_path, method: :get do |f| %>
<%= text_field_tag :search, params[:search], placeholder: "Search Vendors" %>
<%= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}) %>
<%= submit_tag 'Search' %>
<% end %>
routes.rb (make sure to put this near the top)
get 'vendors/search'
vendors/search.html.erb
<% #vendors.each do |vendor| %>
<%= vendor.name %>
<%= vendor.phone %>
<%= vendor.email %>
<% end %>
Ok, You can do it this way:
Go to Your vendor.rb file and add search method like this:
def self.search params
vendors = Vendor.where(category_id: params[:category].to_i) unless params[:category].blank?
vendors
end
Then in Your vendors_controller create search method:
def search
#vendors = Vendor.search(params)
end
Then create form at homepage similar to:
= form_tag search_vendors_path, method: :get, role: 'form' do
.form-group
%label Category
= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}), class: 'form-control'
= submit_tag 'Search'
And do not forget to put
get 'vendors/search'
into Your routes and add "search" view.
You can still do the same without search action, with result on Your homepage. Anyway I hope You got the idea.
When you have a new and create action with the purpose of a simple form that does a calculation (tableless). How do you show what was entered in the text boxes after the create action redirect_to the the new action, so the user sees their existing values.
The controller looks like this
def new
#sum = Sum.new
#result = session[:result]
end
def create
#sum = Sum.new(sum_params)
if #sum.valid?
result = Sum.calculate_total(#sum.first_number, #sum.second_number)
session[:result] = result
redirect_to new_sum_path
else
session[:result] = nil
render action: 'new'
end
end
private
def sum_params
params.require(:sum).permit(:first_number, :second_number)
end
end
The view looks like this
<h1>Result = <%= result %></h1>
<%= form_for #sum do |f| %>
<%= f.label :first_number %>
<%= f.text_field :first_number, class: 'form-input', placeholder: '1' %>
<%= f.label :second_number %>
<%= f.text_field :second_number, class: 'form-input', placeholder: '2' %>
<%= f.submit %>
<% end %>
The model has a method for the calcuation
def self.calculate_total(a,b)
a + b
end
User enters numbers in 2 input text_fields (new action)
User hits submit (create action)
Redirects back to new action with results showing using session[:result]
The input text_fields are blank. I would like this to show the
numbers the was entered.
My questions:
When validation kicks in.. the text fields have the user inputted values.. is this because it is contained within the same action and a redirect loses the information?
To save the inputs when the user gets the results and redirects back to the new action I could store it in a session[:first_number] in the new and assign
it if it's valid in the create action, is this okay or is there an easier/better way?
When you redirect to new_sum_path it is forgetting the old sum. If you want to display the old values, you could pass the current #sum.id through:
redirect_to new_sum_path(old_sum_id: #sum.id)
And then in your new action have:
if(params[:old_sum_id])
#sum = Sum.find(params[:old_sum_id])
else
#sum = Sum.new
end
This way, form_for #sum should pick up on those existing values and fill them in as defaults.