Unable to route in ruby on rails as I encounter specific error - ruby

I'm using rails 5.0 and I get the
PostsController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
NOTE! For XHR/Ajax or API requests while trying to input my url/posts.
my routing.rb file is below:
Rails.application.routes.draw do
get 'posts/index' => 'posts#index'
resources: posts
end
my posts_controller.rb looks like below:
class PostsController < ApplicationController
def index
end
end
I want to know specifically what I'm doing wrong. I've just started to learning Ruby on Rails.

Template is missing. You need to create a file ie index.html.erb under directory /app/views/posts/index.html

GET profiles/employees- Should display all the employees
GET profiles/employees/:id should display a specfic employee
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor
routes in ruby on rails behave in the same way as href tag in rails
we use get for link and if we want toi perform any action on button then w e have to use button.
if you have any doubt let me know in detail.
for more details refer this
http://guides.rubyonrails.org/routing.html
if we write only localhost:3000/employees then bydefault our application will redirect to index page.

The issue is template missing you are missing the template file index.html.erb
I have notice that on your snapshot you named the template file as index.html.erb.txt
remove the .txt and rename to index.html.erb Then it should work
Also you don't need to declare the routes as
get 'posts/index' => 'posts#index'
Just declare the resources: posts is enough. You may check the routes using the rake routes command on your rails console

Rename the file index.html.erb.txt to index.html.erb

Related

Creating a custom view

I am trying to create a landing page for an event for people to visit to see the events details. I have created the view, added a route to the event resources and made changes to the controller but something has been done incorrectly.
Here is my code:
routes.rb:
resources :events do
resources :guests
match '/landing_page', to:'events#landing_page', as: :landing_page, :via =>[:get, :post]
# resources :guestlists
end
event_controller:
def landing_page
#event = Event.find(params[:id])
end
When I open the landing page i get the following error:
"ActiveRecord::RecordNotFound (Couldn't find Event without an ID):"
In case anyone else runs into this, I wanted to document Sergio's last comment here which I believe leads to the outcome I think most people will be looking for. Nesting this inside of a member block should get you an ideal outcome:
resources :events do
member do
get :landing_page
end
end
Running rake routes should now show /events/:id/landing_page(.:format) so you can use the same method you use in your show method that just asks for params[:id].
I was wracking my brain for a while on this as rails resources seem to be dwindling on the interwebs.

Render a view's output later via a delayed_job

If I render html I get html to the browser which works great. However, how can I get a route's response (the html) when being called in a module or class.
I need to do this because I'm sending documents to DocRaptor and rather than store the markup/html in a db column I would like to instead store record IDs and create the markup when the job executes.
A possible solution is using Ruby's HTTP library, Httparty or wget or something and open up the route and use the response.body. Before doing so I thought I'd ask around.
Thanks!
-- Update --
Here's something like what I ended up going with:
Quick tip - in case anyone does this and need their helper methods you need to extend AV with ApplicationHelper:
Here's something like what I ended up doing:
av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
av.extend ApplicationHelper #or any other helpers your template may need
body = av.render(:template => "orders/receipt.html.erb",:locals => {:order => order})
Link:
http://www.rigelgroupllc.com/blog/2011/09/22/render-rails3-views-outside-of-your-controllers/
check this question out, it contains the code probably want in an answer:
Rails 3 > Rendering views in rake task

No route matches [GET] "/"...Sometimes

So I'm a bit of a Rails n00b, so I'll apologize if this is really simple. When I access my server from another computer, I get this message:
No route matches [GET] "/"
And if I try to go to my subpages (Well, currently I only have one), I get something along these lines:
Unknown action
The action 'index' could not be found for AwebpageController
But here's the catch: this only happens sometimes. The rest of the time, the standard RoR homepage loads, and going to wwww.mydomain.com/awebpage serves up the page fine.
My Routes.rb looks like this:
Wobsite::Application.routes.draw do
resources :awebpage
end
And awebpage_controller.rb looks like this:
class AwebpageController < ApplicationController
end
And yes, index.html.erb for Awebpage does exist. It's all so simple that I don't understand what's going wrong. Oh, and my webserver is Thin (Not sure if that matters). Thanks in advance for any help!
You might want to add this to the top of your routes file to set the default controller and page for your site (i.e. http://www.mysite.com/):
root :to => "AwebpageController#index"
To remove the default Ruby on Rails webpage you'll also want to delete the index.html file in your /public/ directory.
Also, although not required, in your controller you're missing the function definition for index.
class AwebpageController < ApplicationController
def index
end
end
Normally you'd do application logic and serve up a view in this function; however if you do nothing RoR automatically loads the view associated with the page (index.html.erb).
If after all this you're still having a problem perhaps explicitly add index to the AwebpageController in your routes file; perhaps rails is only mapping www.mysite.com/Awebpage/ to Awebpage/index and not www.mysite.com/Awebpage/index.

Trouble creating custom routes in Ruby on Rails 3.1

I can't seem to set up a custom URL. All the RESTful routes work fine, but I can't figure out how to simply add /:unique_url to the existing routes, which I create in the model (a simple 4 character random string) and will serve as the "permalink" of sorts.
Routes.rb
resources :treks
match ':unique_url' => 'treks#mobile'
Controller
.
.
def mobile
#trek = trek.find(params[:id])
end
Is this because I'm trying to define a custom action on an existing resource? Can I not create custom methods on the same controller as one with a resource?
By the way, when I change routes.rb to match 'treks/:id/:unique_url' => treks#mobile it works fine, but I just want the url to simply be /:unique_url
Update It seems like find_by_[parameter] is the way to go...
I've been playing in console and I can't seem to get any methods to come forward...I can run Trek.last.fullname for example, but cannot run #trek = Trek.last...and then call...#trek.lastname for example. Any clues why? I think this is my issue.
So is there a field on Trek which stores its unique url? If so you should be doing something like this:
#trek = Trek.find_by_url(params[:unique_url])
trek.find_by_unique_url( params[:unique_url] ) # should do the trick
#pruett no, the find_by_XXX methods are generated on-the-fly via Ruby's method_missing call! So instead of XXX you can use any of the attributes which you defined in a model.
You can even go as far as listing multiple attributes, such as:
find_by_name_and_unique_url( the_name, the_unigue_url)
Check these pages:
http://guides.rubyonrails.org/active_record_querying.html
http://m.onkey.org/active-record-query-interface
if you get a undefined method ... for nil:NilClass , it means that the object you are trying to call that method on does not exist, e.g. is nil.
You probably just missed to put an if-statement before that line to make sure the object is non-nil
Hmm. I usually would do something like this:
map.connect "/:unique_url", :controller => "treks", :action => "mobile"
Then in that controller the ID isn't going to be applicable.. you'd need to change it to something like this:
def mobile
#trek = trek.find_by_unique_url(params[:unique_url])
end
(that's if unique_url is the column to search under)

No route matches controller

In my rails 3 app, I have a route which shows up as follows while calling rake routes:
topic_snippets GET /topics/:topic_id/snippets(.:format) {:action=>"index", :controller=>"snippets"}
In routes.rb
resources :topics do
member do
get 'get_topics'
end
resources :snippets, :only => [:index]
end
In my view, I am referencing this route as follows (where #name = "snippets"):
<%= send("topic_#{#name}_path")%>
When executing the previous line, I get the following routing error, not sure why:
No route matches {:controller=>"snippets"}
Update: I found another question whose responses seem to imply that the above should work: Dynamically construct RESTful route using Rails
Thanks
Anand
OK, I found it - Ryan's comment provided the clue.
I wasnt passing in #topic, which is required. If I remove #topic, it tries to just get at /snippets/ which doesn't have a route. I set #topic to a valid topic before calling this line and it works. Thanks, Ryan!
Have you tried
<%= send(eval("topic_#{#name}_path"), #topic)%>

Resources