I'm working in a rails project and I add the following route:
get '/courses/:invitation_code' => "courses#find_invitation"
On my controller I have the following action:
def find_invitation
#course = Course.where(["invitation_code = ?", params[:invitation_code]])
if !#course.empty?
respond_to do |format|
format.html
format.json { render json: #course, success: true, error: false }
end
end
end
But, when I try to go to localhost:3000/course/demo123 I get the following error:
ActionController::RoutingError at /courses/demo123 uninitialized
constant CoursesController
And I don't understand why. This a project with devise and a new in this project, so I don;t know if I have to do something else, in order to make this action work.
Thanks in advance for your help.
You must specify controller's namespace in your route.
Try changing:
get '/courses/:invitation_code' => "courses#find_invitation"
to:
get '/courses/:invitation_code' => 'admin/courses#find_invitation'
Use this:
match '/courses/invitation_code' => "courses#find_invitation"
convert your root as follows
match '/courses/:invitation_code' => "courses#find_invitation"
The url must be with the segment id as follows
localhost:3000/courses/demo123
Related
I have implemented an AJAX call that renders a JSON response.After checking my Firebug Console, I can see that the call results in a 404 Not Found error.
In my Controller (test_rules_controller.rb)
def rule_attributes
#test = RuleModel.find(params[:rule_model_id]).rule_attributes
respond_to do |format|
format.json { render :json => #test }
end
end
In my Routes:
get "/rule_models/:rule_model_id:/rule_attributes" => "test_rules#rule_attributes", :as => "rule_attributes", :format => :json
I have also created a rule_attributes.json.jbuilder file in my test_rules view folder:
json.rule_attributes #test
I am not sure why it is resulting in this error as I've never used JSON with routes before (Rails Newbie). When I try to navigate to the same URL in the browser, it raises the error that no route matches the given route (http://localhost:3000/rule_models/1/rule_attributes.json).
Can anybody please help?
I'm using Rails 4 with strong parameters to try to find a user by a parameter called "provider_id".
The hope is that I'll be able to make a call with my API to a URL such as:
url.com/api/v1/user?provider=facebook?provider_id=12345
My routes are as follows: routes.rb
namespace :api do
namespace :v1 do
resources :users
match '/:provider/:provider_id', to: 'users#find_by_provider', via: 'get'
end
end
My Strong parameters are:
def user_params
params.require(:user).permit(:name, :age, :location, :provider, :provider_id) if params[:user]
end
My Function is:
def find_by_provider
#user = User.find(params[:provider_id])
respond_to do |format|
format.json { render json: #user }
end
end
Currently, I'm testing with:
url.com/api/v1/facebook/12345
and it is returning:
"{"provider"=>"facebook",
"provider_id"=>"12345"}"
which is good! But I now get the error: "Couldn't find User with id=12345"
Also, somewhat related: occasionally I receive an error that says "param not found: user".
Any suggestions? Thanks!
Change:
#user = User.find(params[:provider_id])
To:
#user = User.find_by(:provider_id => params[:provider_id])
find method will alyways search objects with the id column. Use the where method to search by other criterias/columns
Use:
#user = User.where(provider_id: params[:provider_id]).take
Take a look at http://guides.rubyonrails.org/active_record_querying.html if you want to learn more about the active record query interface.
This is a perfect example where to use find_by! (note the !).
#user = User.find_by!(:provider_id => params[:provider_id])
It works like find_by and returns one User. But if the user is not found it raises an ActiveRecord::RecordNotFound error. That exception is handled by Rails automatically and is turned into a 404 error page.
I want to setup a non-resourceful route in rails but I dont know how. Rails api says the structure has to be like this. post 'post/:id' => 'posts#create_comment' however, I'm not sure what I should exatly write.
I want it to post to the method "addbank" which is in the bankacctscontroller
I will be on the page localhost:3000/bankaccts/new
def addbank
if (params['customer_uri'])
current_user.customer_uri = (params['customer_uri'])
end
if current_user.save
redirect_to root_url, :notice => "bank account added"
else
render json: {error: "Payment account could not be configured properly"}, status: 401
end
end
There are many formats for defining custom routes. The most elaborate one is:
<METHOD> 'PATH' => 'Controller#Action', :as => path_helper_name (:as is optional)
So for your problem it would be :
post '/bankaccts/:id' => 'bankaccts#addbank'
If you use rails4.0,it will be written like this:
get "/bankaccts/new", to: "bankaccts#new", as: :new_post
I suggest you should learn rails routing first via the website "http://guides.rubyonrails.org/routing.html"
I'm trying to add an action called rollback to controller.
As I've seen, the only things I should do is writting the new action:
def rollback
puts "ROLLBACK!"
respond_to do |format|
format.html # index.html.erb
format.json { render json: #components }
end
Modify the routes.rb file:
resources :components do
collection do
post :rollback, :as => 'rollback'
end
end
And calling the action from some view:
<%= link_to 'Rollback', rollback_components_path %>
But I get the following error:
Couldn't find Component with id=rollback
app/controllers/components_controller.rb:18:in `show'
That's because instead of going to rollback action, the controller thinks that we are trying to 'show' to component with id 'rollback'.
Something that it seems weird for me is that calling 'new' action rails uses new_component_path (without s, in singular), but if I write rollback_component_path it throws me an error and I cant see the view.
In your routes you require a POST, just clicking a link is by default a GET, so either write
resources :components do
collection do
get :rollback
end
end
and then the link_to will work as expected.
I am assuming the rollback operation is not idempotent, so a POST is semantically better in that case.
If you write your link as follows, then rails will create an inline form for you:
link_to 'Rollback', rollback_components_path, :method => 'post'
Hope this helps.
This will work
routes.rb
resources :components
match "components/rollback" => "components#rollback", :as => :rollback
In views
<%=link_to 'Rollback', rollback_path%>
Very noob question here.
I am trying to make a digg-like website and when they click a button I want a counter to go up, just like in digg. So I did:
<%=button_to("vote", :action => "vote")%>
and then in my controller I made a action:
def vote
#article = Article.find(params[:id])
#article.votes = #article.votes + 1
respond_to do |format|
format.html { redirect_to(#article.company) }
end
end
When I do that you I get the error:
No route matches {:action=>"agree", :controller=>"companies"}
What should I do?
In a terminal type "rake routes", then look at your routes to find what path you need to use to vote for an article.
Then use
<%= button_to "Vote", vote_path(:id => article.id) %>
Just change the "vote_path" to the path in your rake routes output.
If it's not already in your rake routes file, put something like this in
match "vote/:id" => "controler_name#vote", :as => :vote
Take a look at http://guides.rubyonrails.org/routing.html#adding-more-restful-actions