简体   繁体   中英

Rails - Redirect to specific record page

I'm pretty new to Ruby on Rails and Ruby in general but I'm trying to make a small website with simple database in Ruby on Rails. At the moment I have the html.erb pages to show, add and edit records. The next thing i wanted to do is the action that redirects user to a page with more info about the record he clicked in the record table. I can't really think of any way to do this. Any help would be really appriciated.

ps Sorry for any mistakes in my English - it's not my first language and im still learning!

Here is my html code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tablecontainer">
    <table class="table table-bordered table-condensed">
      <tr class="success">
        <td><b>Nazwa</b></td>
        <td><b>Obrażenia</b></td>
        <td><b>Typ</b></td>
        <td><b>Waga</b></td>
        <td><b>Zasięg</b></td>
        <td><b>Szybkość</b></td>
        <td><b>Rzadkość</b></td>
        <td><b>Opcje</b></td>
      </tr>
      <% @biala.each do |b| %>
          <tr>
            <td><%= b.nazwa %></td>
            <td><%= b.obrazenia %>%</td>
            <td><%= b.typ %></td>
            <td><%= b.waga %></td>
            <td><%= b.zasieg %></td>
            <td><%= b.szybkosc %></td>
            <td><%= b.rzadkosc %></td>
            <td><%= link_to '', {id: b.id, action: 'db_wiecejbiala'}, class: "glyphicon glyphicon-info-sign" %><%= link_to '', {id: b.id, action: 'db_edytujbiala'}, class: "glyphicon glyphicon-pencil" %> &nbsp;&nbsp;&nbsp; <%= link_to '', {id: b.id, action: 'usunbiala'}, data: {confirm: 'Jesteś tego pewien?'}, class: "glyphicon glyphicon-remove" %></td>
          </tr>
      <% end %>
    </table>

And here is the controller:

class BazaController < ApplicationController

  def db_bronbiala
    @biala = BronBiala.all
    @iloscbiala = BronBiala.count
  end

  def db_dodajbiala
    @nowybiala = BronBiala.new
  end

  def utworzbiala
    @nowybiala = BronBiala.new(parametrybiala)
    if @nowybiala.save
      redirect_to(action: 'db_bronbiala')
    else
      render('db_dodajbiala')
    end
  end

  def parametrybiala
    params.require(:bron_biala).permit(:nazwa, :obrazenia, :typ, :waga, :zasieg, :szybkosc, :rzadkosc, :zalety, :wady, :ciekawostki, :opis)
  end

  def usunbiala
    usuwaniebiala = BronBiala.find(params[:id]).destroy
    @biala = BronBiala.all
    render('db_bronbiala')
  end

  def db_edytujbiala
    @biala = BronBiala.all
    @edytowanabiala = BronBiala.find(params[:id])
  end

  def aktualizujbiala
    @biala = BronBiala.all
    @edytowanabiala = BronBiala.find(params[:id])
    if @edytowanabiala.update_attributes(parametrybiala)
      redirect_to(action: 'db_bronbiala')
    else
      render('db_edytujbiala')
    end
  end

  def db_wiecejbiala
    @biala = BronBiala.all
    @bialawiecej = BronBiala.find(params[:id])
  end


end

And the db_bialawiecej code:

<div class="content">
<h2>Lista:</h2>

<div class="tablecontainer">
  <table class="table table-bordered table-condensed">
<tr class="success">
  <td><b>Nazwa</b></td>
  <td><b>Obrażenia</b></td>
  <td><b>Typ</b></td>
  <td><b>Waga</b></td>
  <td><b>Zasięg</b></td>
  <td><b>Szybkość</b></td>
  <td><b>Rzadkość</b></td>
</tr>
<% @bialawiecej.id do |b| %>
    <tr>
      <td><%= b.nazwa %></td>
      <td><%= b.obrazenia %>%</td>
      <td><%= b.typ %></td>
      <td><%= b.waga %></td>
      <td><%= b.zasieg %></td>
      <td><%= b.szybkosc %></td>
      <td><%= b.rzadkosc %></td>
    </tr>
<% end %>

</div>
</div>

On click send id of clicked item (GET). you will have link similar to : localhost:3000/desired_model/5

then in action do @desired_model = DesiredModel.find(params[:id])

redirect user to desired show page.

Show data.

Next time please provide some code :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM