开发者

how to connect my model to my app

Hey all,(im a beginner in rails)

i've created a controller that look like that:

class HomeController < ApplicationController

  def homepage
  end

  def showmsg
  @postword = params[:p]
  end

end

the showmsg view looks like that:

<%= @postword %>

and my homepage view looks like that:

  <%= form_tag( {:controller => 'home', :action => 'showmsg'}, :method => "post") do %>
  <%= text_field_tag(:p,@postword) 开发者_开发百科%>
  <%= submit_tag("post") %>
  <% end %>

now i have a form that i can write something in it and it will show on the showmsg view. i created a model with the param :posts with a :description "text" field too. MY QUESTION is how do i implement the model in the code so any thing i write will be in a list with the things i wrote before, because now (obviously) anything if i write something its deleting the one i wrote before.

thank you all!


I would argue that you're approach is not very rail's like... so if you're learning rails... you're learning it wrong.

Make a Model. Call it "Message":

rails generate model Message content:string

remember to migrate (hopefully you have your databases setup properly):

rake db:migrate

Then in your controller, when you post, you can create message like this:

def create #instead of showmsg... 'create' is the standard name for this
  Message.create(params[:message])
  @messages = Message.all
end

This will create the message in the database, and then it will get all the messages out of the database and put them into @messages.

You need to edit your form so that it uses form_for. You need to pass it @message, which is an instance of Message.new that your first controller action created. You should call this new

In your create.erb.html file, you show all the messages like this:

<ul>
  <% @messages.each do |message| %>
    <li><%= message.content %></li>
  <% end %>
</ul>

I actually wouldn't recommend showing all the messages in the create action - it should really happen in the index action and you should redirect... but we need to keep this simple. Just google this or watch some of Ryan's screencasts and you'll get it.

And you're done. This is the "Rails Way" to do things. It's best to learn it the way they want you to learn it.

I would also commend that you format your code properly by indenting, and start naming your methods to be real english. For example, showmsg is bad and show_message is a lot better.

If all of this is totally confusing, then just create a new project, and then type:

rails generate scaffold message content:string

It will basically build the application you want and a lot more. You can just read the code and see how they did it.

Hope it helps.


Your approach is not really rails like so some tweaks and fixes are needed. Suggestions: check rails approach to REST. The following code will work it is a little more rails like, but still not all the way there.

Generate a model

rails generate model Message postword:string

this will generate the model and create the migration necessary to create the table in the database.

Create the table

rake db:migrate

Define a post action

It will save the postword in the database. In your controller:

def create
  @message = Message.create!(params[:message])

  if @message.save
    redirect_to "/home/showmsg"
  else
    render :action => "/home/homepage"
  end
end

Create and instance of Message to use in your form def homepage @message = Message.new end

Fix your form tag

<%= form_for @message, :url => "/home/create" do |f| %>
  <%= f.label :postword %>
  <%= f.text_field :postword %>
  <%= f.submit "Create" %>
<% end %>

Now let's show the words in the showmsg page

In the controller select the postwords from the database:

def showmsg
  @postwords = Message.all
end

Showing them: /showmsg.html.erb

<H1>postwords list</H1>
<ul>
  <% @postwords.each do |p| %>
    <li><%= p.postword %></li>
  <% end %>
</ul>

Your routes.rb file will have this routes:

get "home/homepage"
get "home/showmsg"
post "home/create"


Define an attribute :new_text in a way similar to this:

class TheModel
  # Virtual writer - everything assigned to this attribute
  # will be added to self.text
  #
  def new_text=(v)
    self.text += v.to_s
  end

  def new_text
    "" # This is write-only attribute
  end
end

Now, use the field 'new_text' in your form.

Of course, this is a very simple example. You should decide whether you want to add the content on every call to :new_text=, maybe some validation would help, the read accessor may need some care, and so on.

For some good guides which may help you start, see the site http://guides.rubyonrails.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜