Rails Create and Edit on the same view - ruby-on-rails

I am trying to accomplish Create and Edit on the same view (and same controller) on Rails 3.2 and Ruby 1.9.2. I have a partial and I am using form_for and the standard form helpers.
I submit using Ajax, so my page (form) is not refreshed or redirected. When the user tries to edit the form, naturally, Rails will create an entry instead of updating it. I was thinking of modifying the Controller's create method to detect existing entries, but I am not sure if this is the correct approach. Thanks.

You can pretty easily include the same form in both create and edit pages by storing it in a partial (typically _form.html.erb) and rendering the form in both create and edit.
This is what rails generate scaffold MODEL FIELD:TYPE FIELD1:TYPE1 will give you, as well.

Related

Parameters for generating controllers in ruby on rails?

When using the command:
rails generate controller home index
When generating a controller do we always have to have a view be generated? Also, can a controller and a view be generated on its own?
No, you do not always have to generate the action. In your case, if you just wanted to generate an empty controller, you can have:
rails generate controller home
Alternatively, controllers and views can be generated manually.
Maybe you're are getting the scaffolding wrong. It should provide you a basic skeleton for beginning and in the later advances on your work, you will start creating it by yourself.
Every time you need to create something custom means you are advancing so scaffolding will stop fitting your needs.

Rails: scaffold. Create admin controller for model

I want to create admin interface for existing model User.
What is the simplest way?
I think, it's scaffolding.
Scaffolding in rails is generated by script(rails generate scaffold), and there is no options to do it dynamically (like in django), correct?
So how can I create scaffolding controller AdminUsers for model User? Create AdminUsers scaffolding and replace AdminUser model by User?
What should I do if model will be changed by migrations? Manually update scaffolding controller and views, right? Is there any automation?
You can generate the files dynamically using scaffolding- the scaffold command can take a namespaced argument.
rails generate scaffold Admin::User
You don't need another model though- your Admin::UsersController and views should be working with the User model.
Using standard solutions such as activeadmin or rails_admin is the simpliest way. If you want something custom and still wondering how to scaffold controller AdminUsers for model User, you might want to take a look at rails-admin-scaffold gem which automates this process and an article with more detailed explanation.
ActiveAdmin is a neat user interface for your Rails application. It gives you a fully customizable user interface to your models.
Site: http://activeadmin.info/
Demo: http://demo.activeadmin.info/admin
Railscasts Episode: http://railscasts.com/episodes/284-active-admin

Simple form in Rails Without Database

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)

creating a simple html page in ruby

Following the ruby tutorial I am trying to create a simple html page.
I will need a controller, i think for some presentation manipoulation but not DB.
I thought that I would create a model, controller and view but then I see that
rails generate model mainMenu freeData1:string freeData2:string
creates the db script.
In order to achieve a simple managed html page that would not require db, what should I create ?
controler only? what is the best practice?
what methods should I put in it for it to be displayed?
thanks.
Yes, you'll just want a controller for the pages which will also create a views folder for your new controller that you can put HTML/ERB/whatever files in.
Since youre not working with the db, you can probably skip the need of a model.

Scaffold default files are the best practice?

Hey, i have some experience with MVC. but I'm new to rails. I'm using the scaffold command to generate some default files. The model looks clean and nice, but the controller and the views aren't really dry. The contents of new.html.erb and edit.html.erb are almost the same and the methods new/edit and create/update are doing almost the same thing. In other frameworks i've used only one view for updating and creating new entries and also the same method in my controller by setting the id as an optional parameter. Do they use this structure to keep things RESTful (i have not much of a clue about rest :()? Is it the best practice to use this default stuff for crud?
The scaffold generator is a pretty good place to start. As you pointed out, there are some things which are not that great about it. I think most people take what the scaffold generates and then fix it up to their liking. For example, you can extract the form part from new.html.erb and edit.html.erb and place it in a partial _form.html.erb. Then update new.html.erb and edit.html.erb to include that partial to render the form. I think that for Rails 3, the scaffold generator has been changed to do this by default.
It does seem like new and edit, and create and update are pretty much the same, but you need to remember that they are mapped to different HTTP methods and URLs, which ties in to the whole RESTful resource idea. Check out the RailsGuides for routing, the section CRUD, Verbs, and Actions has a nice table of the seven different routes and the differences between them.
You should check out ryanb's nifty-generators:
http://github.com/ryanb/nifty-generators
The scaffolding creates a partial view called _form that then gets referenced from the new and edit views. It also comes with a bunch of other nice options -- like generating your views in haml or your tests in Shoulda or RSpec.
If they wanted you to keep it they wouldn't call it "scaffolding." It's just there to make everything work out of the box. If you put it into production you will more than likely be laughed at.

Resources