I have query to the db, where I ask for specific entries. This is the controller:
def index
@contributions = Contribution.all
@number1 = params[:number1]
@number2 = params[:number2]
@itemsok = Contribution.where("first_item_id = ?",@number1).where("first_item_grade = ?",@number2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @contributions }
end
Ok and I have a form in the view wich allows me to pass data from the user input
<%= form_tag(contribution_path, :method => "get") do %>
<%= label_tag(:number1, "First Item Id") %>
<%= text_field_tag(:number1) %>
<%= label_tag(:number1, "First Item Grade") %>
<%= text_field_tag(:number2) %>
<%= submit_tag("Do it!" , :remote => true) %>
<% end %>
That works perfectly, but now I want to render this function from the main page index (display_controller, display index.html.erb). I gave all the properties to the display_controller like this:
class DisplayController < ApplicationController
def index
@items = Item.all
@contributions = Contribution.all
@number1 = params[:number1]
@number2 = params[:number2]
@itemsok = Contribution.where("first_item_id = ?",@number1).where("first_item_grade = ?",@number2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @contributions }
end
end
end
And I have the same form in the display view that is in the contributions view….
Right now what It does is that it redirects me to the contributions part, what I want t achive is to render the results in the display view…
Any thoughts? Thank you very much.
Sorry for my terrible english.
#
I got it
Got it the answer was to change in view the form to selected action
<%= form_tag(contribution_path, :method => "get") do %>
<%= label_tag(:number1, "First Item Id") %>
<%= text_field_tag(:number1) %>
<%= label_tag(:number1, "First Item Grade") %>
<%= text_field_tag(:number2) %>
<%= submit_tag("Do it!" , :remote => true) %>
<% end %>
Change it to
<%= form_tag(display_path, :method => "get") do %>
<%= label_tag(:number1, "First Item Id") %>
<%= text_field_tag(:number1) %>
<%= label_tag(:number1, "First Item Grade") %>
<%= text_field_tag(:number2) %>
<%= submit_tag("Do it!" , :remote => true) %>
<% end %>
In my case this was not working at the beginning but now I figured that it was because I have my display_path as the root_path so I changed it to…
<%= form_tag(root_path, :method => "get") do %>
<%= label_tag(:number1, "First Item Id") %>
<%= text_field_tag(:number1) %>
<%= label_tag(:number1, "First Item Grade") %>
<%= text_field_tag(:number2) %>
<%= submit_tag("Do it!" , :remote => true) %>
<% end %>
And now it works perfectly.
Can someone change this question to answered, I have like 21 of reputation and can’t answer my own question. Tanks.
For better code quelity you should avoid
repititionsof code and make reusable, move that formm to a commonpartial viewand use:url =>{:action => 'my_action'}in the form tag then you can use it anycontrollerindexview. While form action it will take thedefaultcontrollerfrom which it will be rendered.and in controller respective index view
you can even declare variables for action and controller in your index method like
and then in form