Forum: Ruby on Rails Bind two pages by form_tag

Bdf83ae096f116bda2d4a74daca45dfa?d=identicon&s=25 Marie Bellec (Guest)
on 2014-01-30 14:47
(Received via mailing list)
Hello !

I try to bind two pages together, a page which serves to make a research
and a page which posts the result of the research.

Here are two pages that I want to bind together :

http://zupimages.net/up/14/05/0efm.png (Index)
http://zupimages.net/up/14/05/901g.png (Show)

And here is what it currently gives me when I press the Search button :

http://zupimages.net/up/14/05/cr2r.png (Index)
http://zupimages.net/up/14/05/f2mn.png (Show)

Here is the code of the two pages:

Index.html.erb :

<h1>Search counter</h1><%= form_tag '/counters/id', method: :get
%><table>
    <tr>
      <th>Identifiant</th>
      <th><%= search_field_tag "counters" %></th>
      <th><%= submit_tag 'Search' %></th>
      <th></th>
    </tr></table>

Show.html.erb :

<p id="notice"><%= notice %></p>
<p>
  <strong>Nombre de message :</strong>
  <%= @counter_msg %> </br>

  <strong>Maximum de message pouvant être envoyé :</strong>
  <%= @counter_max %> </br>

  <strong>Reset automatique :</strong>
  <%= @counter_reset %> </br>

  <strong>Date du dernier reset :</strong>
  <%= @counter_reset_date %> </br></p>

<%= link_to 'Edit', edit_counter_path(@counter) %> |<%= link_to 'Back',
counters_path %>

I would like to have your help to know where the problem comes from and
how
I could fix it. (I think this is a story of road, but being a beginner I
do
not know)

Thank you in advance for your help
0048ae7fd903196c7d78ed58492a736c?d=identicon&s=25 Antônio Augusto Sousa Britto (Guest)
on 2014-01-30 22:51
(Received via mailing list)
could you provide the counters_controller.rb code? Seems that your
variable
raw_counter is not set properly...



2014-01-30 Marie Bellec <mariejbellec@gmail.com>

> And here is what it currently gives me when I press the Search button :
>       <th>Identifiant</th>
>   <%= @counter_msg %> </br>
> <%= link_to 'Edit', edit_counter_path(@counter) %> |<%= link_to 'Back',
counters_path %>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/rubyonrails-talk...
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>



--
Att, Antnio Augusto de Sousa Britto - 43 turma de Cincia da Computao -
UFU
Bdf83ae096f116bda2d4a74daca45dfa?d=identicon&s=25 Marie Bellec (Guest)
on 2014-01-31 11:16
(Received via mailing list)
Here is the counters_controller.rb code :

class CountersController < ApplicationController
  #require 'colorize'

  before_action :get_counter, only: [:show, :edit, :update, :destroy]

  # GET /counters
  # GET /counters.json
  def index
  end

  # GET /counters/1
  # GET /counters/1.json
  def show

    raw_counter = REDIS.hget(params[:id], 'c')
    raw_counter_reset = REDIS.hget(params[:id], 'overquota_reset_c')
    raw_counter_reset_date = REDIS.hget(params[:id],
'overquota_reset_at')
    #@counter_max = 'put'.red
    #@counter_msg.red
    @counter = {:id => params[:id]}
    if raw_counter == '0'
      @counter_msg = '0'
      @counter_max = '0'
    else
      @counter_msg = raw_counter[9..17]
      @counter_max = raw_counter[1..6]

      #if @counter_msg > @counter_max

      #end
    end

    if raw_counter_reset == "1"
      @counter_reset = 'oui'

    else
      @counter_reset = 'non'
    end

    @counter_reset_date = Time.at(raw_counter_reset_date.to_i)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @counter }
    end



  end

  # GET /counters/new
  def new
    @counter = Counter.new
  end

  # GET /counters/1/edit
  def edit
  end

  # POST /counters
  # POST /counters.json
  def create
    @counter = Counter.new(counter_params)

    respond_to do |format|
      if @counter.save
        format.html { redirect_to @counter, notice: 'Counter was
successfully created.' }
        format.json { render action: 'show', status: :created, location:
@counter }
      else
        format.html { render action: 'new' }
        format.json { render json: @counter.errors, status:
:unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /counters/1
  # PATCH/PUT /counters/1.json
  def update
    respond_to do |format|
      if @counter.update(counter_params)
        format.html { redirect_to @counter, notice: 'Counter was
successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
      format.json { render json: @counter.errors, status:
:unprocessable_entity }
      end
    end
  end

  # DELETE /counters/1
  # DELETE /counters/1.json
  def destroy
    @counter.destroy
    respond_to do |format|
      format.html { redirect_to counters_url }
      format.json { head :no_content }
    end
  end

  private

    # Use callbacks to share common setup or constraints between
actions.
    def get_counter
      @counter = REDIS.hget(params[:id], 'c')

    end

    # Never trust parameters from the scary internet, only allow the
white
list through.
    def counter_params
      #params.require(:counter).permit()
    end
end


I don't think the problem comes from contôler and raw_counter,
because when I replace :

<%= form_tag '/counters/id', method: :get %>

by:

<%= form_tag '/counters/ovm%3Ab%3A%7Bpausecho%7D%2Fmozilla', method:
:get %>

of index.html.erb,the application works, and shows it :

http://zupimages.net/up/14/05/gogk.png

with this URL :

http://127.0.0.1:3000/counters/ovm%3Ab%3A%7Bpausec...
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-01-31 11:55
(Received via mailing list)
On Friday, January 31, 2014 10:15:08 AM UTC, Marie Bellec wrote:
>
>
That's because your controller is looking for params[:id], but your form
tag is always setting it to 'id' (because the url is /counters/id),
which
presumable results in redis returning nil. (The value of your search
field
is in params[:counters])

It might also be prudent to check what is returned from redis before
using
it.

A form can't change the path submitted to based on the fields submitted,
so
you either have to have some dummy value (if you want it to go to the
show
action), or else let it go to the index action, but if the :counters
parameter is present, indicating that a search is being requested, then
redirect to the appropriate show page.


Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.