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 %>
Related
When I edit a model with nested attributes everything works perfectly. If I try to create a new record with nested attributes, the nested attributes don't appear on the form that has the nested attributes. I have two models as follows
class JobSpec < ActiveRecord::Base
has_many :availabilities
accepts_nested_attributes_for :availabilities, allow_destroy: true
end
and
class Availability < ActiveRecord::Base
belongs_to :job_spec
end
in the job_specs_controller I have
class JobSpecsController < ApplicationController
def new
#job_spec = JobSpec.new
#availabilities = Availability.none
availability = Availability.new
availability.job_spec = #job_spec
#availabilities << availability
end
def edit
#job_spec = JobSpec.find(params[:id])
#availabilities = #job_spec.availabilities
end
def job_spec_params
params.require(:job_spec).permit(
... all of the job_spec attributes,
availabilities_attributes: [:id, ... all of the availability attributes],
)
end
end
In _form for job_spec I have
<% #availabilities.each do |availability| %>
<%= f.fields_for availability do |builder| %>
<%= render partial: 'availabilities/form_mini',
locals: {f: builder, availability: availability} %>
<% end %>
<% end %>
Since editing works, I assume there's not much wrong. Any ideas?
Update
I've almost sorted out all the problems but still have one. Here's what I've done.
1) I had to build the job_spec availabilities. The new action of the JobSpecsController now looks like this
def new
#job_spec = JobSpec.new
#job_spec.client_id = params[:client_id]
#job_spec.availabilities.build
end
2) I wanted to be able the destroy the nested models, so I had to pass :_destroy in with the nested attributes to job_spec_params. That method looks like this
def job_spec_params
params.require(:job_spec).permit(
... all of the job_spec attributes,
availabilities_attributes: [:id, :_destroy, ... all of the availability attributes],
)
end
end
3) Finally, I had to change _form for job_spec. I have
<%= f.fields_for :availabilities do |builder| %>
<%= render partial: 'availabilities/form_mini', locals: {f: builder} %>
<% end %>
The only issue I have now is that I want to pass and instance variable through to my availability partials. Something like this
<%= render partial: 'availabilities/form_mini', locals: {f: builder, a: availability} %>
How should that be done? I'm not sure how to iterate over all the #availabilities while rendering all the partials.
Glad you worked some of the issues out. First one small improvement
#job_specs_controller.rb
def new
#job_spec = JobSpec.new(client_id: params[:client_id])
#job_spec.availabilities.build
end
And to overcome your last issue, the solution is a mix of what you had before and after. You can pass the actual object in the fields_for method. It's like that:
#_form.html.erb
<% #job_spec.availabilities.each do |availability| %>
<%= f.fields_for :availabilities, availability do |builder| %>
<%= render partial: 'availabilities/form_mini',
locals: {f: builder, a: availability} %>
<% end %>
<% end %>
I am creating a website using a rails and I would like to share the model. I followed The tutorial for creating an article. In the ArticlesController is index, show, new, edit, create, update and destroy. I figured I would use this as the data entry part of my app. Then I was going to create a PageController that has home, news, videos, music and events. Here is my set up:
here is my Article model
class Article < ActiveRecord::Base
has_attached_file :image, styles: { large:"700x700>", medium:"300x300>", thumb:"150x150#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
validates :title, presence: true,
length: { minimum: 5 }
belongs_to :page
end
here is my page model
class Page < ActiveRecord::Base
has_many :article
end
here is my page controller
class PageController < ApplicationController
def home
#page = Page.includes(:articles)
end
def news
end
def videos
end
def music
end
def events
end
end
here is my home view
<h1>Page#home</h1>
<p>Find me in app/views/page/home.html.erb</p>
<%= #page.articles.each do |article| %>
<%= article.title %>
<%= article.content %>
<%= article.image %>
<% end %>
here is the error I get
undefined method `articles' for #<ActiveRecord::Relation []>
not clear onwhats going on. Any help would be appreciated
From the limited information you posted, I am assuming the best thing for you to do is create an association. Pages should has_many articles and articles should belong_to a page. http://guides.rubyonrails.org/association_basics.html
After that is setup, your controller can be set up as follows:
#page = Page.includes(:articles)
This will load your pages and its related articles in a single SQL query, then you can call
#page.articles.each |article|
puts article.title
puts article.content
puts article.image
end
You can now access all the information and use it as you please.
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.
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.