I have a guidelines model and a comments model and I'm trying to get them to both be trackable in my activities feed. Guidelines is working, comments is not.
The 2 main issues are:
I'm unsure how to add 'Comment' as a second trackable_type in the activities_controller.rb and I'm unsure how to sort my view in comment/_create.rb - it should say that a comment was added to x guideline (and link that guideline show page).
guidelines.rb
include PublicActivity::Model
tracked owner: ->(controller, model){controller && controller.current_user}
attr_accessible :content, :hospital, :title, :user_id, :guideline_id, :specialty, :updated_by, :current_user, :subtitle, :slug, :activities, :comment
belongs_to :user
has_many :favourite_guidelines
has_many :comments, :dependent => :destroy
comments.rb
include PublicActivity::Model
tracked owner: ->(controller, model){controller && controller.current_user}
belongs_to :guideline
belongs_to :commenter, class_name: 'User'
activities_controller.rb
def index
#activities = PublicActivity::Activity
.order("created_at desc")
.where(trackable_type: 'Guideline' 'Comment')
views/public_activity/comment/_create.html.erb
added a comment
<% if activity.trackable %>
to the guideline <%= link_to activity.trackable.body, activity.trackable %>
<% else %>
which can no longer be viewed
<% end %>
views/public_activity/guideline/_create.html.erb
added a guideline
<% if activity.trackable %>
titled <%= link_to activity.trackable.title, activity.trackable %>
<% else %>
which can no longer be viewed
<% end %>
my routes.rb is
Guidelines::Application.routes.draw do
get "activities/index"
# get "user/index"
ActiveAdmin.routes(self)
devise_for :admin_user, ActiveAdmin::Devise.config
get "guidelines/topic"
get "guidelines/topichospital"
get "guidelines/topicspecialty"
get "guidelines/favourite"
get "profiles/show"
get "guidelines/show"
devise_for :users
devise_scope :user do
get 'signup', to: 'devise/registrations#new', as: :register
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
get 'edit', to: 'devise/registrations#edit', as: :edit
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get 'about', to: 'about#about', as: :about
end
resources :guidelines
get 'guidelines', to: 'guidelines#index', as: :guidelines
get 'favourites', to: "favourites#show", as: :favourites
get 'topics', to: 'guidelines#list', as: :topics
get 'hospitals', to: 'guidelines#listhospital', as: :hospitals
get 'specialties', to: 'guidelines#listspecialty', as: :specialties
resources :activities
root :to => 'guidelines#index'
resources :guidelines do
resources :comments
end
If I understood properly your question this cast explains it very well:
http://railscasts.com/episodes/406-public-activity in case you haven't seen it yet.
In my case I have Post model (instead of your Guidelines) and Comment and I just added
include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_user }
to both Post and Comment model and everything worked well without adding anything to activities_controller
public_activity/comment/_create.html.erb
said "<%= activity.trackable.body.truncate(100) %>" on <%= link_to activity.trackable.commentable.title, project_post_path(activity.trackable.commentable.project, activity.trackable.commentable.id) %> post.
public_activity/post/_create.html.erb
added a new <%= link_to activity.trackable_type.downcase, project_post_path(activity.trackable.project, activity.trackable.id) %> to <%= link_to activity.trackable.project.title, project_path(activity.trackable.project) %> project.
activities index.html.erb
<% #activities.each do |activity| %>
<%= link_to activity.owner.full_name, activity.owner if activity.owner %>
<%= render_activity(activity) %>
<% end %>
Related
I am getting following error :
undefined method `recommendations_path' for #<#<Class:0x0078>>
I have Recommendation model
class Recommendation < ActiveRecord::Base
belongs_to :user
belongs_to :recommended_user, class_name: "User", foreign_key: :recommended_user_id
end
I have user model
class User < ActiveRecord::Base
has_many :recommendations
................
end
In recommendation controller
def new
#recommendation = current_user.recommendations.new
end
In new.html.erb
<%= form_for #recommendation do |f| %>
<%= f.text_field :relationship %>
<%= f.text_field :comment %>
<%= f.submit %>
<% end %>
My routes, where I think problem is:
devise_for :users
resources :users, only: [:show] do
collection do
get :customer_signup
get :employee_signup
end
member do
get :choose_role
get :become_a_customer
get :become_a_employee
end
end
resources :users do
resources :recommendations
end
Thats actually when the form is trying to identify the path for your #recommendation.
According to your routes.rb your form must be:
<%= form_for [:user, #recommendation] do |f| %>
I hope I am not asking an obvious question/ wont get down voted to hell for this.I am working on rails app and I am getting a "param is missing or the value is empty" error.
I have an event and questions that have already been created and I am using a nested form to allow the user to answer all the questions at once.
I am using rails 4
Models
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
Routes.rb
Rails.application.routes.draw do
root 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :answers
resources :users, only: [:new, :create]
resources :questions do
resources :answers #-> domain.com/questions/1/answers/new
end
resources :events, only: [:index, :new, :show, :update] do
patch ":id", action: :index
collection do
get :favorite
get "question/:id", action: :question
end
end
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
answers controller
class AnswersController < ApplicationController
def new
#event = Event.find(params[:id])
#answer = Answer.new
end
def show
end
def create
#answer = Answer.new(answer_params)
#answer.save
redirect_to events_path, notice: "Answered Questions"
end
private
def answer_params
params.require(:answer).permit(:response, :question, :event, :user, :event_id, :question_id, :user_id)
end
end
This is where my issue lies. Originally I had a very generic nested from a la http://railscasts.com/episodes/196-nested-model-form-revised but I switched the form_for down to the #answer because that is whats being created and switched to a button_to because the submit button was not writing the answer to the DB.(I believe it was trying to trigger something with #event )
<h1>New answers</h1>
<%= fields_for #event do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :questions do |b| %>
<p>
<%= b.text_field :poll%><br />
<%= form_for #answer do |x| %>
<%= x.text_field :response %>
<% end %>
</p>
<% end %>
<%= button_to "New", action: "create"%>
<% end %>
<%= link_to 'Back', answers_path %>
Please let me know if you need anymore code or have any questions
Thanks!
UPDATE
I have reworked my code based off this blog post http://iroller.ru/blog/2013/10/14/nested-model-form-in-rails-4/
now I am running the update through the events controller or at least I'm trying to.
The code is as follows, the error im getting now is
undefined local variable or method `event_params' for #
Thanks guys and girls sorry for the dumb questions
Models
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
Routes.rb
Rails.application.routes.draw do
root 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :answers
resources :users, only: [:new, :create]
resources :questions do
resources :answers #-> domain.com/questions/1/answers/new
end
resources :events, only: [:index, :new, :show, :update] do
patch ":id", action: :index
collection do
get :favorite
get "question/:id", action: :question
end
end
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
methods from events_controller
def question
#event = Event.find(params[:id])
end
def update
#event = Event.find(params[:id])
if #event.update(event_params)
redirect_to events_path, notice: "Answers saved"
else
redirect_to events_question_path, notice: "Answers not saved"
end
questions.erb
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<%= f.object.name %>
<%= f.simple_fields_for :questions, f.object.questions do |q| %>
<%= q.object.poll%>
<%= q.simple_fields_for :answers, q.object.answers.build do |a|%>
<%= a.text_field :response %>
<% end %>
<%end %>
<%= f.button :submit%>
<% end %>
I want to a searching function for users. It does not work. So I simplified the method, I just want to refresh the index page when I hit the search button. But it still does not work, it said
ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=search.
please tell me Why
my controller
class UsersController < ApplicationController
load_and_authorize_resource :except => [:index]
def search
redirect_to users_path
end
end
My view
<%= form_tag users_search_path, :method => 'get' do %>
<td><%= text_field_tag :username, params[:username] %></td>
<%= submit_tag "Search", :class => "buttons buttons-rounded buttons-flat-action", :id => "button-new"%>
<br><br><br>
<% end %>
My Route
Procedures::Application.routes.draw do
devise_for :users
#### USER MANAGEMENT ####
resources :users do
resources :rateofpays # professional timesheet
resources :roles
resources :biographies
resources :qualifications do
collection do
put 'complete', :action => 'complete'
end
end
resources :supervisors
end
#### users search ####
get 'users/search' => "users#search", as: 'users_search'
The line
get 'users/search' => "users#search", as: 'users_search'
...is too far down in your routes. the resources :users appears first, and it has a match path that looks like users/:id and the users/search is incorrectly matching against that.
Just move the get 'users/search' to the top... or alternatively define it as a collection method under resources :users
resources :users do
collection do
get 'search'
end
When I try to subscribe to a product by clicking the link:
<%= link_to "Subscribe", :controller => "products", :action => "subscribe_product", :id => product.id, :method => :post %>
I get this error and notice the parameters are wrong.
ActiveRecord::RecordNotFound in ProductsController#show
Couldn't find Product with id=subscribe_product
{"id"=>"subscribe_product", "method"=>"post"}
My subscribe_product method in my ProductsController is:
def subscribe_product
#product = Product.find(params[:id])
#product.subscriptions.create(:subscriber_id => current_user.id)
end
My route:
resources :products do
post :subscribe_product, :on => :collection
end
These are the associations:
class User
has_many :products
has_many :subscriptions, :foreign_key => :subscriber_id
class Product
belongs_to :user
has_many :subscriptions, :as => :subscribable
class Subscriptions
belongs_to :subscriber, :class_name => "User"
belongs_to :subscribable, :polymorphic => true
Users subscribe in another controller:
PagesController
def index
#product_history = current_user.products
end
end
pages/index.html.erb
<% for product in #product_history %>
<%= product.name %>
<%= product.price %>
<%= link_to "Subscribe", :controller => "products", :action => "subscribe_product", :id => product.id, :method => :post %>
<% end %>
So why is my action method being seen as the ID instead?
Try :
resources :products do
post :subscribe_product, :on => :member
end
It will generate routes like :
subscribe_product_product POST /product/:id/subscribe_product(.:format) {:action=>"subscribe_product", :controller=>"products"}
and use path like in view :
subscribe_products_path(product.id)
Since you're passing an id, the subscribe_product route should be a member route. Try this, and let me know what you get:
resources :products do
member do
post 'subscribe_product'
end
end
In the controller (to get around non-mass-assignable attributes):
def subscribe_product
#product = Product.find(params[:id])
subscription = Subscription.new
subscription.subscriber_id = current_user.id
#product.subscriptions << subscription
end
Please try this. Change your route to :
resources :products do
post :subscribe
end
Then change your link like :
<%= link_to "Subscribe", subscribe_products_path(:id => product.id), :method => :post %>
In my routes i have:
resources :accounts do
resources :transfers
put '/transfers/:id(.:format)' => 'transfers#accept'
end
In my model:
class Transfer
include DataMapper::Resource
belongs_to :account
belongs_to :alias_from, "Alias"
belongs_to :alias_to, "Alias"
class Account
include DataMapper::Resource
belongs_to :user
has n, :transfers
In my view:
<% #transfers_in.each do |income|%>
Amount: <%= income.amount%> <%= income.account.currency%>
<% form_for ([???, income]), :as => :transfer, :url => {:controller=>'transfers', :action => 'accept'} do |f|%>
Choose the account <%= f.collection_select :account, #accounts, :name, :name %>
<%= f.submit :value => "Accept" %>
<% end %>
<% end %>
How should I call for the account here, if here #transfers_in is called by other association?
#aliases = #owner.aliases.all()
#transfers_in = #aliases.transfers_in.all()
I've tried something like
<% #acc = Account.all()%>
<% #trs = #acc.transfers.get(:id => income.account)%>
<% form_for ([#trs, income]), ....
but that gave me
No route matches
{:controller=>"transfers",
:action=>"accept"}
In rake routes such route exists.
Would be thankful for any help.
In your routes, you should have better results using the macros Rails provides for routing. Instead of doing the old-style route map, try:
resources :accounts do
resources :transfers do
put 'accept', :on => :member
end
end
The router is really smart when it comes to RESTful routes, but when you start manually mapping things, it can get confused... especially when you're doing it inside of nested, RESTful routes.