rails default actions are missing params in routing - ruby

Excuse me for my lame question, but i`m stuck.
Plain and simple in my routes.rb file i have:
resource :books do
resource :reviews
end
running rake routes | grep reviews gives me:
books_reviews POST /books/reviews(.:format) reviews#create
new_books_reviews GET /books/reviews/new(.:format) reviews#new
edit_books_reviews GET /books/reviews/edit(.:format) reviews#edit
GET /books/reviews(.:format) reviews#show
PUT /books/reviews(.:format) reviews#update
DELETE /books/reviews(.:format) reviews#destroy
My question is: where is the :id param in the show and edit actions? According to this tutorial: http://guides.rubyonrails.org/routing.html there should be "id" params in the routing, like this:
new_books_reviews GET /books/:id/reviews/new(.:format) reviews#new
edit_books_reviews GET /books/:id/reviews/edit(.:format) reviews#edit
GET /books/:id/reviews(.:format) reviews#show
On top of that where are the routes for show, update and destroy actions?
I think i`m missing something fundamental, because this really sucks. Running Rails 3.2.11
Thanks for the help

Try using resources instead of resource.
Rails thinks there's only one when you use the singular form and doesn't need the ID.
For more info see the "singular resource" section of the guide

Using resources over resource will make sure Rails accounts for many resources.
Your routes should look like this
resources :books do
resources :reviews
end

Related

about Sinatra on multiple Resources

Sorry to ask such a simple question but I didn't see any possible answer in internet.
I just wondering in Sinatra, can I write something like:
get '/users/:user_id/posts/:id' do
xxx
end
just like rails? cause when I write this on my rb file, sinatra keep telling me it didn't know this ditty.
thank you
A (horrible) example from one of our legacy systems.
get '/is_type/:type/id/:id' do
store.valid_for_type?(params)
end
Multiple placeholders in a route is totally fine in Sinatra. It sounds like your config.ru is wrong, you're issuing a POST, or you're calling the wrong endpoint entirely. Can you update your post with an example of the request you are issuing?

Loading a Rails project from a different Rails project

I would like to create an application (X) which will analyse model details of a given application.
Ex: Say I have a Rails app called blog, and it has classes like post
#post.rb
#db columns -> title, desc
class Post < ActiveRecord::Base
end
So in my X application , when the path is given of blog, it should read the models of the blog and should come up with its stats (something like Ruby code quality tools).
Example: it should return the columns if the Post class, like List.columns
But my problem is, since my X application is in a different environment than the blog application, how can I load the blog application models and access the functionality as I'm running the blog application from its console itself?
Because lots of Ruby code quality gems are reading the source code as string, but what I want is to use its functionalities as its accessing from the console.
This sounds like the perfect set of requirements for using an API. Just set up your blog controllers to respond to json and then you can make API calls from X to blog to get the information you need.
There are quite a few resources out there for creating an API within your controllers. For Rails 3, I'd recommend Rails 3 In Action by Ryan Bigg and Yehuda Katz: http://manning.com/katz/. There are also numerous blogs, tutorials, etc if you search around.

Is there a template for a website that accepts an uploaded file, does something, and lets the user download the result?

I have a few Ruby scripts that process text files in different ways, that many of my friends find useful. However, most of the people I know are not comfortable running scripts on the command line. The easiest thing for them would be to create a simple webpage where people could upload a file, select a few options, have it processed, and then download the result.
I know it wouldn't be too hard to build something like this in Rails or Merb or something like that, however it seems like a very common problem, so I was wondering if there was already some kind of template, or similar application that I could easily modify, i.e. let the user upload a file, choose a few options, then {fill in code to do something with file}, let the user download the resulting file?
In the past I used Carrierwave to upload user avatars.
If you are used to Rails it's really straightforward.
Let it be a TextFile resource:
gem 'carrierwave'
$ rails g scaffold textfile content:string title:string etc etc
$ rails g uploader textfile
class TextFile < ActiveRecord::Base
attr_accesible :content
mount_uploader :content, TextFileUploader
end
And that is is pretty much all you have to do to obtain the app's skeleton. However, to answer your real question, no, I don't think there is already a rails app that does exactly that.
https://github.com/jnicklas/carrierwave
I found sinatra-fileupload, which pretty perfectly answers my question. It's a very minimalistic framework which works perfectly, I can just plug in the file handling, and change the layout etc a bit. There were many examples of sophisticated Rails plugins linked to databases, with versioning and stuff, but I really wanted the most minimal example.

How to turn my rails app into static content?

My rails app fetches a bunch of xml feeds once a day, loads them into the db and then displays them in aggregate. I'm thinking that I can save on server memory if I just output the pages as static files and let them be served directly by the front-end server (nginx in my case). I asked in an IRC room and was told to not use rails and create the files using rake tasks. However, I'm wondering what the easiest way would be to go about doing this. Layout, asset files and content are in different places in rails obviously, so I guess I would need to combine the layout and content and then insert the css/javascript.
Any thoughts/ideas are welcome.
[Solved]
I ended up using the examples from render_to_string from a rake task and made some tweaks to get the following code inside my rake task:
views_path = Rails.root.to_s + "/app/views"
av = ActionView::Base.new(views_path)
av.class_eval do
include ApplicationHelper
end
products = Product.all
a = av.render(:template => "products/show", :layout => "layouts/application", :locals => { :#products => products } )
This then renders both the template and the layout, and allows the use of the #products instance variable inside the template just as you would if you were using a controller.
Then I just need to write the output of the render to a file.
For a task like this you can use Rails' built in caching mechanisms.
There is another stack overflow post which shows some example code of how to build code to write that cache manually from something like a rake task.
Perhaps middleman or jekyll could be used?
I've only used middleman, but you could use a rake task and support script to get the latest xml feeds and stick that into middlemans data dir (i.e. data/feeds.yml), then use your existing layouts to render that yaml file. Middleman and rails share a lot of similar tech for rendering etc.
You'd have to modify your layouts a little bit.
You could probably find gems to replace yaml with something else if you wanted.

Sinatra - how do I get the server's domain name

I'm trying to get the domain name in my Sinatra app but as a newbie I really am struggling to figure out how to do this, and I know it must be possible!
Rack::Request#host_with_port looks promising, but I don't know how to get this from my app - how do I get stuff from Rack in my Ruby code?
Or is there another way - I'm thinking I don't really want to do this every time a request happens (although it's not too bad), but I thought it'd be better if I could just do it once when the application loads up.
Any hints?
simply use request.host inside your code.
get "/" do
puts request.host #=> localhost
end
Take a look at:
request.env.inspect
so you can see all the request environment variables.
I think that you are looking for
request.env["SERVER_NAME"]

Resources