I am creating a simple RoR application where I can create posts and also comment on them. My thought on how to do this is to have posts and comments be of the same type and utilizing the same MVC except comments have a parent id. Posts will have a parent id of 0 while comments will have the id of its parents. I was thinking of when I tried to create a comment, I could just pass in the parent's id to the new method. This did not work. Although I got no errors it seems like the #post variable in the new method is not the same #post variable in the create method. My intuition was that the new method creates a new object then passes the object to the view. The view then populates the objects parameters and then sends it to the create method and the create method saves the object to the database. From trial and error this does not seem to be the case unless I am just doing it wrong. Is there an easier way to get the same functionality I am trying to achieve? or is there a way to get my way to work? any help would be greatly appreciated!
I would recommend you follow the classic "Build a Blog in 15 minutes" video that shows you how to do this.
Among other things, since a post has_many :comments I wouldn't recommend doing what you're proposing.
Related
I am working through my first Ruby on Rails App!
I have followed along with ruby's getting started tutorial and created a blog. http://guides.rubyonrails.org/getting_started.html
I am trying to implement a way to track when articles are viewed so I created a second table/model/controller that I'd like to insert the current date every time an article is viewed (show).
I'm not really sure how to implement this though... my initial though was in the article controller under the show def I would do something like
#view = View.new(view_params)
#view.Save!
This didn't really work. Any idea at all?
Thanks
You didn't mention what columns you had in your new model, but I'm assuming it's just the default timestamps and a field for the article ID.
So, within the 'show' action for the articles, you could do something like this. Your action names and such may differ, but the principle is the same.
def show
#article = Article.find(params[:id])
View.create(article_id:#article.id)
end
I can't think of any reason why you'd need to store the view itself in an instance variable, or any variable for that matter.
Suppose I've a model : Thing
class Thing < ActiveRecord::Base
attr_accessible :name, :url
In my coffeescript, I've a function generating an array of JSON objects, eg:
[{"url":"https://www.example.com/1","name":"1"},
{"url":"https://www.example.com/2","name":"2"},
{"url":"https://www.example.com/3","name":"3"}]
That function is called when the user click on a button in the index page of Thing.
What I want is create multiple Thing resources based on the JSON objects generated by the coffeescript function.
What is the best way to do it?
I'm considering using Ajax to redirect to the create action of Thing but not sure this is the best way.
Thanks
Yes, AJAX is the way to go. What you'd do is submit these objects to the controller where you'd make your Thing models.
If you're submitting all of those objects and once and want them all created in one shot you could do that in the create action or you could do that in a create_all action. I like the idea of a create_all action because it's letting us know it's not a simple create action where people have learned to assume it just makes 1 of an object. This is a personal preference though.
I just started trying to get acquainted with Rails, and I don't really know much Ruby. Currently I'm doing a beginners project to get acquainted with the framework which involves a simple form which takes a name, email, and phone number, and when you hit a submit button the page should refresh and present the information you submitted (so there is no database interaction and the model is supposed to do very little). It's very simple, but as my current knowledge of Ruby is pretty minimal, I'm getting somewhat confused. I've written the views for the most part but I'm still confused on what to put in the controller and the model. If anyone could provide any hints that would be great!
You need tableless model. Please refer excellent webcast from Ryan on this topic.
http://railscasts.com/episodes/193-tableless-model
Models (at least those descending from ActiveRecord::Base) require a database, so if you are using a model you are using a database, even if its a simple one like SQLite.
If you don't want to descend into models and generating a migration to create the tables for your models, then you are probably just going to store the form values into instance variables and reference those in your view. Below is a quick example how to do that.
If you have a form that sends data (ex: name and email) to a create action, the create action would look sort of like this:
def create
#name = params[:name]
#email = params[:email]
render :my_template
end
The create action above is assigning the params sent to it to instance variables, which you can then reference in your view. In the above example the create action is going to try and render a view called my_template which would probably be named my_template.html.erb and might look something like this:
<p>Your name is <%= #name => and your email is <%= #email =>.</p>
This is an extremely small and contrived example, but hopefully this helps.
When you move on to working with models your create action might instead create a new instance of a model, pass that model the params sent by the user, save the model, and then redirect them to a page that shows the data.
It is much easier with a database.
rails new stack
cd stack
rm public/index.html
rails generate scaffold Member name:string email:string phone:string
rake db:migrate
rails server
Then browse to localhost:3000/members
(Assuming you are using rails 3)
I understand why a Rails index method would use the plural form of a resource - we're showing all projects, for example.
And I understand why the show method would use the singular form - we only want to see one project, with a particular ID.
But I don't understand why new and create would use the plural. Is there a way to create more than one project at a time? Is there some other reasoning for using the plural here that someone could explain?
New and Create aren't plural, in the way I think about REST. Instead, I think about it like:
whatever.com is your base domain, and whatever.com/books means that you have a collection of resources each named book. The collection itself is named books.
So, when you want to create a new book, you are asking the collection for the information needed to create a new book. This becomes /books/new
When you actually create the book, you are posting information to /books. The HTTP verb is POST, so when you POST to your collection, you execute the create action.
This looks like a good starting point on REST.
I thought they were always plural. Scroll down a bit on this page for an example of the routes generated by resources :photos
Whether you're GETting a single resource or POSTing to the collection, you're still in the domain of photos. So, search the domain of photos given an id, POST a new photo to the domain of photos, etc.
I'm using STI in Rails, and I've got a Vehicle object, that has many different types of subclasses, like Car, Truck, etc. It's for a simple app, so STI works fine in this case, but I'm having trouble creating a single form where any type of Vehicle record can be created.
Using the following routing:
resources :vehicles
resources :cars, :controller => 'vehicles'
resources :trucks, :controller => 'vehicles'
I can have /cars and /trucks routing set up, and both pointing to the same form. However, since the form is pointing to the vehicles controller, and generating a Vehicle object for the form, it has no way to know that the /cars url should create a Car object.
I'm trying to get a routing system set up where /cars would point to a form that would intrinsically know to make a object for the form using either Car.new or even Report.new(:type => "Car"). I thought about working a routing system like /vehicles/:subclass, and somehow using params[:subclass] in the controller, but I also can't figure out how to do that sort of routing and still avoid other routing errors caused by Rails' STI magic.
I could always parse the URL to get the value, but that seems like an unsafe and hacky way to go about it.
I'm curious if anyone has any advice or experience on the Rails way to do this. Thanks!
Since you want to use the same form for all vehicles, then I'm assuming all the fields are the same except for the object type. Then why not have a combo box in the form to allow the user select what type of object the user want to create?
You can then handle the proper object persistent in the create action in the controller.