Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoyutorius/f2858714a54548ef3bba to your computer and use it in GitHub Desktop.
Save jacoyutorius/f2858714a54548ef3bba to your computer and use it in GitHub Desktop.
Railsで更新も編集も削除も、全部indexページでやりたい ref: http://qiita.com/jacoyutorius/items/ea0673fe30cfe7cdac8f
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Edit', users_path(id: user.id) %></td>
$ rails g scaffold User name:string age:decimal note:text
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to request.referer, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Rails.application.routes.draw do
root "users#index"
resources :users
end
def index
@users = User.all
@user = User.new #追記
end
redirect_to users_path
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to users_path, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def index
@users = User.all
if params[:id].present?
set_user
else
@user = User.new
end
end
# -------------ここから----------------
<h1>Listing users</h1>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :age %><br>
<%= f.text_field :age %>
</div>
<div class="field">
<%= f.label :note %><br>
<%= f.text_area :note %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
# -------------ここまで----------------
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Note</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.age %></td>
<td><%= user.note %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
resources :users, expect: [:show, :new, :edit]
resources :users, expect: [:show, :new, :edit]
[
{name: "Spike Spiegel", age: 27, birth: "2044-06-26", note: "SwordFish2"},
{name: "Jet Black", age: 36, birth: "2035-12-03", note: "Hammerhead"},
{name: "Faye Valentine", age: 77, note: "1994-08-14", note: "redtail"},
{name: "Edward Wong Hau Pepelu Tivrusly 4", age: 13, birth: "2058-01-01", note: ""},
{name: "Ein", age: 2}
].map{|row| Hashie::Mash.new(row) }.each do |data|
User.create!(
name: data.name,
age: data.age,
birth: data.birth,
note: data.note
)
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to request.referer, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment