2

I know this is probably a very basic question but I am brand new to Ruby and kinda in a dead end. I have made a simple little site with profiles and profile pages. But on the profile pages I would like to add a new text field like "Bio" for instance where the user types in a bio about himself and it shows. Im just at a blank on how to create a new text field where people can input this stuff. I know this is basic stuff just stuck and looking for some help or guidance to a tutorial or something. Thank you in advance

N.Smith
  • 43
  • 1
  • 4

2 Answers2

0

Here's an example copied from another answer:

<%= form_for(:ad, :url => {:action => 'create'}) do |f| %>
  <%= f.text_field(:name) %>
  <%= f.text_area(:text, "", :size => "50x10") %>
  <%= submit_tag("Submit") %>
<% end %>
Community
  • 1
  • 1
user212514
  • 3,110
  • 1
  • 15
  • 11
0

This is kind of a complicated question, once you think about it, because there are so many parts.

Ruby on Rails is built on a architecture, called Model View Controller or MVC. The three parts together make the user interface that is presented to the user.

Models are the actual data, like the User objects, in this case. To create the model, type in this command to the console:

rails g model User bio:text name:string

This will make a basic user model, which only contains two columns, a column for the bio, and a column for their name. Note that this is very uncomplicated, and this can be expanded on a lot, but for now it will do.

Or, if you already have a user model, type in this command to the console:

rails g migration add_bio_to_users bio:text

Next are the controllers, controllers are, in a way, what connect the models and the views, so they manage all of the logic in the back end, like creating new users, or adding bios to their profiles.

You can create the user controller like this (if you do not already have one):

rails g controller Users new

And then, you can add this code to the new file generated, to add the functionality of adding bios (and showing them, too) (and updating other columns as well):

def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
        render @user
    else
      render @user # Handle error accordingly
    end
end

def show
    @user = User.find(params[:user])
end

private
    def user_params
      params.require(:user).permit(:name, :bio)
    end

Now, to the final part, which is the view, which is the actual thing that is presented to the user:

<%= form_for(:user, :url => {:action => 'update'}) do |f| %>
    <%= f.text_field(:name) %>
    <%= f.text_area(:bio, "", :size => "50x50") %>
    <%= f.submit yield(:button_text) %>
<% end %>

Note that this is just a simple view that assumes that you also have a column name in your User model, you can change this accordingly.

And, finally, to show the user, add this to the show view:

<%= @user.bio %>

to show the bio in the show view.

Good luck!

Saketram Durbha
  • 450
  • 3
  • 14
  • Thank you so it all works great up until when I want to call it. So I have the form where you can type the bio in and I hit submit. But on my show where I have <%= @user.bio %> nothing is being shown. – N.Smith Apr 14 '16 at 23:30
  • Is there an error being thrown, can you show me your code? – Saketram Durbha Apr 14 '16 at 23:47