Shapes in ruby ​​on rails, passing variables between one form to another

I am trying to pass variables between forms, and unfortunately I am not managing

in mine index.html.erb, I have a text box with a name SearchInput, and I would like to pass this variable to the following form search.html.erbso that I can fill in some data accordingly. How can i do this?

In mine index.html.erbI have the following <% form_tag (:action => 'search') do %>

I really don't know how to do this, please, any advice would be really appreciated

+3
source share
3 answers

, Model, View Controller MVC, Rails.

, :

HTTP- Rails URL-. , , . Rails - , params, , . , ( , "@" ), , .

, - POST URL- . .

Rails params. . params .

. , , , .

  • http://localhost:3000/products , GET.

  • Rails .

  • Rails ProductController # ( , app/controller/products_controller.rb)

  • , Rails / (app/views/products/index.html.erb), , ProductController. :

    <% form_tag(:action => 'search') do %>
      <%= text_field_tag "SearchInput" %>
      <%= submit_tag %>
    <% end %>
    
  • SearchInput POST http://localhost:3000/products/search

  • Rails .

  • Rails ProductsController #, params , SearchInput SearchInput. .

    def search
      @products = Product.find_by_name(params["SearchInput"])
    end
    
  • , Rails / (app/views/products/search.html.erb) :

    <% if @products.empty? %>
      No products found
    <% else %>
      <% @products.each do |product| %>
        <%= link_to product.name, product_url(product)%> 
        <br />
      <% end %>
    <% end %>
    
+29

params[..]. , , .

<form action="..." method="...">
  <input type=".." name="search_input" ... />
  ...
  ...
  <button>submit</button>
</form>

def action_name
  @collection = Model.find(:conditions => {:column_name => params['search_input']})
  ...
  ...
end
+2

link_to, , , , . :

, @foo, , .

<%= link_to "Link Text", new_widget_path(:foo => @foo) %>

@foo [: foo], params [: foo] .

, Rails WidgetController.

def new
  @widget = Widget.new(:foo => params[:foo])
end

Widget : foo, [: foo].

new.html.erb Widget foo, :

 <%= form_for(@widget) do |f| %>
  <%= f.label :other_attribute %><br />
  <%= f.text_field :other_attribute %>
  <%= f.hidden_field :foo %>
  <%= f.submit %>
<% end %>

Widget other_attribute foo!

0

Source: https://habr.com/ru/post/1723627/


All Articles