Help me connect to MySQL Database

30 views
Skip to first unread message

Nguyen Le

unread,
Jan 20, 2014, 5:27:36 AM1/20/14
to rubyonra...@googlegroups.com
Hi all,
I have one 1 *.html.erb. I want to connect to mySQL and show some
information to website(ff or chrome). Could you help me about that?
Thank you so much

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 20, 2014, 5:41:28 AM1/20/14
to rubyonra...@googlegroups.com
On 20 January 2014 10:27, Nguyen Le <li...@ruby-forum.com> wrote:
> Hi all,
> I have one 1 *.html.erb. I want to connect to mySQL and show some
> information to website(ff or chrome). Could you help me about that?

If you want to use Rails then start by working right through a
tutorial such as railstutorial.org, which is free to use online.

Colin

Nguyen Le

unread,
Jan 20, 2014, 6:06:50 AM1/20/14
to rubyonra...@googlegroups.com
Thanks for your reply, Colin. I asked this question, because I want to
make sure file *.html.erb can connect to mySQL database. Could you help
me to answer this question and give me code sample?. Thank you so much

Walter Lee Davis

unread,
Jan 20, 2014, 9:08:40 AM1/20/14
to rubyonra...@googlegroups.com

On Jan 20, 2014, at 6:06 AM, Nguyen Le wrote:

> Thanks for your reply, Colin. I asked this question, because I want to
> make sure file *.html.erb can connect to mySQL database. Could you help
> me to answer this question and give me code sample?. Thank you so much
>

Colin's reply will help you understand the folly of your question as written. A single erb file won't connect to a MySQL database on its own, within the context of a Rails application, ever. The View is responsible for showing the data, not fetching it or responding to user input.

If you have a full Rails application there, and not just a single erb file, you may have a different question. But until you come to grips with the basics of how Rails works, I doubt you're going to get much of an answer here that makes sense to you until you can formulate your question in another manner.

Walter

> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/069854e0823d328441f7550a03df0d66%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.

jsnark

unread,
Jan 20, 2014, 9:10:33 AM1/20/14
to rubyonra...@googlegroups.com
*.erb files do not connect to databases.  Models do.  Follow Colin's advise and work through the tutorial.

Jordon Bedwell

unread,
Jan 20, 2014, 9:23:10 AM1/20/14
to rubyonra...@googlegroups.com
On Mon, Jan 20, 2014 at 8:10 AM, jsnark <s...@monmouth.com> wrote:
> *.erb files do not connect to databases. Models do. Follow Colin's advise
> and work through the tutorial.

If *.erb files don't connect to the database in the entire context of
the application (which they do through models which does it through
other stuff) then you are wrong too, because models don't connect to
the database, they go through other stuff (like your view does) and
model out the behavior.

Lehner Viktor

unread,
Jan 20, 2014, 10:36:06 AM1/20/14
to rubyonra...@googlegroups.com

Nguyen Le

unread,
Jan 20, 2014, 11:04:03 AM1/20/14
to rubyonra...@googlegroups.com
The first, I want to say thank to all reply. I think it is very helpful
for me. I will learn Rails step by step. Thank you so much.

Nguyen

jsnark

unread,
Jan 20, 2014, 3:45:32 PM1/20/14
to rubyonra...@googlegroups.com

No, you are wrong.  The model xyz.rb has direct access (through inheritance) to the database table xyzs.  Thus, the method:

def Xyx.get_first
  find(1)
end

returns the row of table xyzs with id=1 with no qualification.  In a controller, the statement find(1) is meaningless.  You have to reference the model to get access to the table as in Xyz.find(1).  Views (*.erb files) should never directly reference models.  They get their database information from the controller.

Jordon Bedwell

unread,
Jan 20, 2014, 4:23:08 PM1/20/14
to rubyonra...@googlegroups.com
On Mon, Jan 20, 2014 at 2:45 PM, jsnark <s...@monmouth.com> wrote:
>
>
> On Monday, January 20, 2014 9:23:10 AM UTC-5, Jordon Bedwell wrote:
>>
>> On Mon, Jan 20, 2014 at 8:10 AM, jsnark <s...@monmouth.com> wrote:
>> > *.erb files do not connect to databases. Models do. Follow Colin's
>> > advise
>> > and work through the tutorial.
>>
>> If *.erb files don't connect to the database in the entire context of
>> the application (which they do through models which does it through
>> other stuff) then you are wrong too, because models don't connect to
>> the database, they go through other stuff (like your view does) and
>> model out the behavior.
>
>
> No, you are wrong. The model xyz.rb has direct access (through inheritance)
> to the database table xyzs. Thus, the method:

Sure it does, if by direct you mean has to go through a client library.

>
> def Xyx.get_first
> find(1)
> end
>
> returns the row of table xyzs with id=1 with no qualification. In a
> controller, the statement find(1) is meaningless. You have to reference the
> model to get access to the table as in Xyz.find(1).

If we throw out the method and apply what the method does then:
Sure I do, unless... I go ***through*** the same library that ActiveRecord does.

> Views (*.erb files)
> should never directly reference models. They get their database information
> from the controller.

Sure they shouldn't because `@user = User.where(:id => session[:uid])`
in the controller isn't how most programmers go about it, most of them
decorate it all into neat and tidy formatted objects that create
completely indirect access. An instance of an object in a variable
set inside of the controller and accessible in the view is not an
indirect access. But maybe you meant to say you should never
initialize that object in the view as it's the views job to transform
that object into something meaningful not to pull that data into that
object and all the other things we go on about.
Reply all
Reply to author
Forward
0 new messages