Kjakforum - Tráður
Næsti tráður ->
Kjakforum -> Annað -> Kreativt -> ruby, search method... Helpz?

Innlegg: 5
Síða [1]
PSnake







Innlegg: 2869
# Tráður - 10/05/2012 klokkan 16:43
Vil gjarna hava at Ruby on Rails fer a eina avisa sidu og kallar mina Search method til at visa avis urslit heldur enn oll Items har. Help?

fara herfra:
<h1>Listing categories</h1>

<table>
<tr>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @categories.each do |category| %>
<tr>
<td><%= category.category %></td>
<td><%= link_to 'Items', items_path %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Category', new_category_path %>


til her:

<h1>Listing items</h1>

<table>
<tr>
<th>Category</th>
<th>Title</th>
<th>Date</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @items.each do |item| %>
<tr>
<td><%= item.category %></td>
<td><%= item.title %></td>
<td><%= item.date %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Item', new_item_path %>
<%= link_to 'My Account', my_account_path %>

<h2>Enter search keyword</h2>
<form action ="items/search" method="post">
<input name = "key" type="input" />
<input value="Search" type="submit"/>
</form>


Sum riggar, men eg vil hava at Search metodan her automatiskt fer i gongd og leitar eftur einum keyword eg koyri inn fra fyrra partinum. Controller til hetta seinna er (Search method sjalv er nidast):

class ItemsController < ApplicationController
before_filter :login_required#, :only => :my_account

# GET /items
# GET /items.json
def index
@items = Item.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @items }
end
end

# GET /items/1
# GET /items/1.json
def show
@item = Item.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @item }
end
end

# GET /items/new
# GET /items/new.json
def new
@item = Item.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @item }
end
end

# GET /items/1/edit
def edit
@item = Item.find(params[:id])
end

# POST /items
# POST /items.json
def create
@item = Item.new(params[:item])
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render json: @item, status: :created, location: @item }
else
format.html { render action: "new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end

# PUT /items/1
# PUT /items/1.json
def update
@item = Item.find(params[:id])
respond_to do |format|
if @item.update_attributes(params[:item])
format.html { redirect_to @item, notice: 'Item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end

# DELETE /items/1
# DELETE /items/1.json
def destroy
@item = Item.find(params[:id])
@item.destroy
respond_to do |format|
format.html { redirect_to items_url }
format.json { head :no_content }
end
end

def search
@items = Item.find(:all, :conditions => ["Category LIKE ?", "%#{params[:key]}%"])
end

end


------------------
>:3 ??? - >9000!!! - :O ?!?!
OG Darl Souls


Síðsta broyting av PSnake klokkan 10/05/2012 klokkan 16:44 (1x Broytt)
Offline
PSnake
Stovnaði tráðin







Innlegg: 2869
# Innlegg: 1 - 10/05/2012 klokkan 19:20
Disregard. Havi funnid eina loysn.


------------------
>:3 ??? - >9000!!! - :O ?!?!
OG Darl Souls


Offline
|
Ishkur


Geekboy





Innlegg: 1196
# Innlegg: 2 - 10/05/2012 klokkan 20:52
eg skuldi akkurát hjálpa tær


Offline
|
PSnake
Stovnaði tráðin







Innlegg: 2869
# Innlegg: 3 - 10/05/2012 klokkan 22:14
Takk fyri tankan :3 Hevdi bidad eftur svarid og arbeitt vid onkrum odrum, umtad ikki var fyri til i morgin :3 Vonadi bara onkur fangadi tad adrenn eg fann okkurt :3

Men havi ein annan trupuleika, um tu ert agrytin um at hjalpa. Destroy method sum kemur vid tad man genererar stuff vid scaffold ger absolutt einki av onkrari orsok. Edit riggar fint, men destroy ger akkurat tad sama sum bara show.

<h1>Listing categories</h1>

<table>
<tr>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @categories.each do |category| %>
<tr>
<td>
<form action ="items/search" method="post">
<input name = "key" value="<%= category.category %>" type="input" readonly="readonly" />
<input value="Items" type="submit"/></form>
</td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<form action ="items/search" method="post">
<input name = "key" type="input" readonly="readonly" />
<input value="Show all" type="submit"/>
</form>
<br />

<%= link_to 'New Category', new_category_path %>
<br>
<%= link_to 'Back', my_account_path %>



class CategoriesController < ApplicationController
# GET /categories
# GET /categories.json
def index
@categories = Category.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @categories }
end
end

# GET /categories/1
# GET /categories/1.json
def show
@category = Category.find(params[:id])

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

# GET /categories/new
# GET /categories/new.json
def new
@category = Category.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @category }
end
end

# GET /categories/1/edit
def edit
@category = Category.find(params[:id])
end

# POST /categories
# POST /categories.json
def create
@category = Category.new(params[:category])

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

# PUT /categories/1
# PUT /categories/1.json
def update
@category = Category.find(params[:id])

respond_to do |format|
if @category.update_attributes(params[:category])
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category = Category.find(params[:id])
@category.destroy

respond_to do |format|
format.html { redirect_to categories_url }
format.json { head :no_content }
end
end
end


------------------
>:3 ??? - >9000!!! - :O ?!?!
OG Darl Souls


Offline
|
Ishkur


Geekboy





Innlegg: 1196
# Innlegg: 4 - 10/05/2012 klokkan 23:42
tu skal sløkkja telduna, og lata hana sova í eina nátt. dagin eftir blæsur tú nógv allastaðni runt um hana.. hopi tað hjálpur


Offline
|
Gordon


L33T





Innlegg: 1480
# Innlegg: 5 - 11/05/2012 klokkan 18:22
Kenni einki til Ruby on Rails, eri _akkurát_ byrjaður at hyggja at tí, men

<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>

sær løgi út í mínum eygum.

er

<td><%= button_to "Destroy", :confirm => "Are you sure?", :method => :delete %></td>

kanska tað tað burdi verið?
doesn't hurt to try


Síðsta broyting av Gordon klokkan 11/05/2012 klokkan 18:28 (2x Broytt)
Offline
|
Innlegg: 5
Síða [1]


Tú noyðist at vera innritaður fyri at skriva innihald.