I am trying to integrate forem with thumbs_up. I have inherited the forem Post model and controller.
Here is my controller :-
class PostsController < Forem::PostsController
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
Here is how the Post Controller of Forem looks like :-
module Forem
class PostsController < Forem::ApplicationController
before_filter :authenticate_forem_user
before_filter :find_topic
.
.
.
.
private
def find_topic
#topic = Forem::Topic.find(params[:topic_id])
end
end
end
Here is my routes:-
mount Forem::Engine, :at => "/forums"
resources :posts do
member do
post :vote_up
end
end
Here is my view :-
<%= link_to t('vote for this post!', :scope =>"forem.post"), main_app.vote_up_post_path(#post), :method => :post %>
This is the error which I am getting :-
ActiveRecord::RecordNotFound in PostsController#vote_up
Couldn't find Forem::Topic without an ID
What could be the issue?
Your problem is the before filter:
module Forem
class PostsController < Forem::ApplicationController
#...
before_filter :find_topic
#...
def find_topic
#topic = Forem::Topic.find(params[:topic_id])
end
and then:
class PostsController < Forem::PostsController
def vote_up
#...
So find_topic will be called before vote_up but the route for vote_up won't have a :topic_id; no :topic_id means that find_topic will be doing this:
#topic = Forem::Topic.find(nil)
and that's where your error comes from.
Three options come to mind:
Move vote_up to a separate controller class that doesn't inherit from Forem::ApplicationController.
Add a skip_filter :find_topic, :only => :vote_up to PostsController.
Adjust the route and link to get a :topic_id in the route.
If upvoting doesn't need the #topic then (1) or (2) would work, otherwise you'll have to go with (3).
check rake routesin command prompt,
and check id should be post :vote_up OR get:vote_up – ror_master
and use debugger in controller!
and write params there perhaps you will get solution.
Related
I'm trying to follow wicked tutorial for creating an object partially
( https://github.com/zombocom/wicked/wiki/Building-Partial-Objects-Step-by-Step )
The problem is, I am having trouble creating the object itself. I've tried with and without strong params, or even making the call out of the controller, but can get it passed. What am I doing wrong?
class ProspectsController < ApplicationController
include Wicked::Wizard
steps :signup, :business_details, :user_details
def show
create_prospect if params[:prospect_id].nil?
byebug # => prospect_id is no appearing => Not_found
#prospect = Prospect.find(params[:prospect_id])
render_wizard
end
def update
#prospect = Prospect.find(params[:prospect_id])
params[:prospect][:status] = 'users_detailed' if step == steps.last
#prospect.update_attributes(params[:prospect])
render_wizard #prospect
end
def create_prospect
#prospect = Prospect.create
new_prospect_build_path(prospect_id: #prospect.id)
end
# def prospect_params
# params.require(:prospect).
# permit(:user_first_name, :user_last_name, :user_email, :dni, :plan, :empresa_name, :empresa_email,
# :empresa_phone, :empresa_address, :empresa_web, :empresa_category, :empresa_summary, :user_birthday,
# :user_phone, :user_address, :sex, :iban_code, :status, :prospect_id)
# end
end
Routes:
resources :prospects, only: [:show, :update] do
resources :build, controller: 'prospects'
end
you're using same controller action for two routes:
GET /prospects/:prospect_id/build/:id => prospects#show
GET /prospects/:id => prospects#show
same with update.
If you will get to that controller by GET prospect_path you will not get :prospect_id, but :id.
Hi everybody I'm from the old school using rails 2.
Actually I'm using rails 4 and I'm trying to find a way to create methods on the controller without writting
On RAILS 2 used: (only needed to write the name on the controller)
#controller
def report_a
end
def report_b
end
def report_c
end
...and whatever def
#ROUTES
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
On RAILS 4
#controller
def report_a
end
def report_b
end
def report_c
end
#ROUTES
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
The problem is when I create a view report like this: (views/reports/report_a.html.erb)
<%= form_tag :action=>"report_a" do %>
<% end %>
I get this message:
No route matches [GET] "/reports/report_a"
To resolve this issue and doing Rails instruccions works like this:
#controller
def report_a
#users= User.search(params[:name])
end
def result_report_a
#users= User.search(params[:name])
end
#view/reports/report_a.html.erb
<%= form_tag :action=>"result_report_a" do %>
<% end %>
#routes.rb
get "reports#report_a"
post "reports#result_report_a"
get "reports#report_b"
post "reports#result_report_b"
get "reports#report_c"
post "reports#result_report_c"
Also I found this better way:
#controller reports.rb
def search_report_a
report_a
render :report_a
end
def report_a
#users = User.where(:name=>params[:name])
end
def search_report_b
report_b
render :report_b
end
def report_b
#users = User.where(:address=>params[:address])
end
...
#Routes.rb
resources :users do
match 'search_report_a', :via => [:post,:get], :on => :collection
match 'search_report_b', :via => [:post,:get], :on => :collection
...
end
Is there any other way to create methods without adding all inside ROUTES.RB ?
Any suggestions or the only way is adding get and post?
Imagine a case where you have several methods.
Best approach in Rails is to use REST architecture. Your controller should be able to view, create, update and destroy some resource (of course all actions are not mandatory).
For example:
def ReportsController
def index
# Actions to show links to all possible reports
end
def show
# Show report based on params
end
end
Your #show method may show any of report (report_a, report_b, etc) just by checking param from GET request.
And you don't need to make all logics inside #show method. It would be better to place report-related logic in, maybe, some service objects.
I've searched high and low for this and the other solutions don't seem to help.
The error:
The action 'create' could not be found for WatchedWatchersController
I get this after clicking a link made with this code in a view:
<%= link_to 'Watchlist', { :controller => "watched_watchers",
:action => "create",
:watcher_id => current_user.id,
:watched_id => user.id},
:method => "post" %>
My model for this class is:
class Watched_Watcher < ActiveRecord::Base
attr_accessible :watched_id, :watcher_id
validates :watched_id, :watcher_id, presence: true
end
And my controller is:
class WatchedWatchersController < ApplicationController
def new
end
def create
#ww = Watched_Watcher.new
#ww.watcher_id = params[:watcher_id]
#ww.watched_id = params[:watched_id]
#ww.save
end
def update
end
def edit
end
def destroy
end
def index
end
def show
end
end
I've set up my routes as a RESTful resource:
resources :watched_watchers
And rake routes reveals:
POST /watched_watchers(.:format) watched_watchers#create
So I'm stumped and any help is appreciated. Thanks in advance.
Vimsha's comment solved it
"Rename your model to WatchedWatcher(no underscore)". And then once I fixed the model name I just needed to have a redirect back to the same page.
I am trying to override a model of forem gem so that I could use thumbs_up gem for voting purpose.
I did a rails g model Post and trying to inherit the post model of forem by this line of code
class Post < Forem::Post
acts_as_voteable
end
same for the controller
class PostsController < Forem::Postscontroller
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
I keep getting this error
undefined method `vote_up_post_path'
in my route.rb
mount Forem::Engine, :at => "/forums"
resources :posts do
member do
post :vote_up
end
end
I guess I am doing something really stupid out here and I am not overriding the model correctly. I was using this Clarification on how to use "thumbs_up" voting gem with Rails 3 post to set up thumbs_up
Can someone help??
If I'm getting your question correctly, you wanna change the behavior of forem Post in order to support voting using acts_as_votable.
For that to work you need to re-open Forem::Post class in an initializer (e.g. config/initializers/forem.rb) and add to it acts_as_votable line like this:
module Forem
class Post
acts_as_votable
end
end
And the same for Forem::PostsController:
module Forem
class PostsController
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
end
It seems it was a stupid mistake, realized it while having discussion with patrickmcgraw.
forem hides your routes and and you have to mention main_app before the routes, so after writing
main_app.vote_up_post_path instead of vote_up_post_path the page was up again.
Hope it helps someone trying to use forem.
There are conflict with inherited_resources and Ryan Bates's cancan gem.
I have some simple controller
class IssuesController < InheritedResources::Base
respond_to :html
load_and_authorize_resource
def tag
#issues = Issue.tagged_with(params[:tag]).recent.paginate(:page => params[:page])
end
protected
def collection
#issues = end_of_association_chain.recent.paginate(:page => params[:page], :per_page => Settings.per_page_defaults.issues)
end
end
and route
resources :issues do
collection do
get "tag/:tag" => "issues#tag", :as => "tags"
end
end
Everything looks correct, but on attempt to request http://localhost:8080/issues/tag/tag1
i see
ActiveRecord::RecordNotFound in IssuesController#tag
Couldn't find Issue without an ID
After removing load_and_authorize_resource from controller - everything works fine, but i need access control.
Any idea how to solve this issue?
use load_and_authorize_resource :except => :tag. Note that this wont apply rules. If you need to apply some use authorize! instead.