开发者

interact between controller and view in rails

开发者 https://www.devze.com 2023-03-09 15:26 出处:网络
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.

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

精彩评论

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

关注公众号