开发者

interact between controller and view in rails

I created a form that you can type text with a "post" button开发者_StackOverflow社区 and i want that when you type something and press the post button it will show what you wrote.

My question is: What is the code to connect between the view and the controller?

This is the view i already created:(i also generated a home controller with a showmsg action)

<h1 align="center">MicroBlog</h1>
<br><br>
  <div align="center">
  <%= form_tag( {:controller => 'home', :action => 'showmsg'}, :method => "post") do %>
  <%= text_field_tag(:p,@postword) %>
  <%= submit_tag("post") %>
  <% end %>
  </div>

how should the showmsg action would look like so i could show the msg? thank you very much!


Your controller will need to access the param being sent to it, assign it to an instance variable, which your view will then be able to access.

class HomeController < ApplicationController
  def showmsg
    @message = params[:p]
  end
end

/app/views/home/showmsg.html.erb

...
<%= @message %>
...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜