나는 Ruby on Rails에서 한동안 모험을하고 있지만 js로 아무것도하지 않았다. 나는 인터넷에서 몇 가지 튜토리얼을 검색했지만 내가 원하는 바에 맞는 것은 없습니다.
하나의 뷰에 두 개의 부분이있는 레일스 응용 프로그램이 있습니다. 하나는 양식이고 다른 하나는 양식에 의해 생성 된 작업 목록을 보여줍니다. 제출 버튼을 클릭하면 새로 생성 된 작업으로 부분 목록이 업데이트되는 동시에 이전에 생성 된 다른 작업도 유지되기를 바랍니다.
누구든지 나를 도울 더 나은 링크가 있습니까? 아니면 설명 할 시간이 있습니까?
편집하다:
아래 답변의 도움으로 코드를 다음과 같이 변경했습니다.
_to_do_list.html.erb (부분 양식으로, 아래 양식을 업데이트해야하며, 버튼 하위 양식을 눌렀을 때 나열합니다.)
<%= form_for @todo_list, remote: true do |f| %>
....
<div class="field">
<%= f.label :name%><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label 'Público?'%>
<%= f.check_box :is_public %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<div id= "show_list">
<%= render 'show' %>
</div>
그리고 변경되지 않은 부분 쇼는 업데이트해야합니다.
<table id="list" border="1" >
<tr>
<th>Name</th>
<th>Description</th>
<th>is public?</th>
<th>Add Tasks</th>
<th>erase List</th>
</tr>
<% @todo_lists.each do |f|%>
<tr>
<td><%=f.name %></td>
<td><%=f.description%></td>
<td><%=f.is_public %></td>
<td><%= link_to new_task_path :method => :get %></td>
<td> <%= link_to 'Destroy', f, confirm: 'Are you sure?', method: :delete%></td>
</tr>
<% end %>
</table>
그리고 새로운 js 파일 :
$("#todos").html('<%=j render partial: "to_do_lists/show", locals: { todo_lists: @todo_list } %>');
제어 장치:
def create
@todo_list = ToDoList.new(params[:to_do_list])
@todo_list.member_id = current_member.id
respond_to do |format|
if @todo_list.save
format.js{}
format.html { redirect_to @todo_list, notice: 'Lista ' +@todo_list.name+ ' foi criada com sucesso!' }
format.json { render json: @todo_list, status: :created, location: @todo_list }
else
format.html { render action: "new" }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
아이디어는 페이지를 떠나지 않고 생성 된 새 작업으로 테이블에 새 행을 만드는 것입니다.
감사합니다.
편집하다
필요한 것은 AJAX
. 문서를 보면
JavaScript can also make requests to the server, and parse the response. It also has the ability to update information on the page. Combining these two powers, a JavaScript writer can make a web page that can update just parts of itself, without needing to get the full page data from the server. This is a powerful technique that we call Ajax.
이 작업을 수행하려면 먼저 양식 값을 js가 제출할 수 있도록remote: true
기본적으로 허용 하는 옵션 을 갖도록 양식을 변경해야합니다 . 양식은 다음과 같습니다.
<%= form_for @todo_list, remote: true do |f| %>
...
// form fields
<% end %>
<div id= "some-id"> // to target this div to render your partial from js
<%= render 'show' %> // it doesn't make sense to have your partial inside form and
</div>
그것은 당신의 형태를 벗어나야합니다
생성 작업에서 respond_to
블록 이 필요합니다.
def create
@todo = Todo.new(todo_params)
respond_to do |format|
if @todo.save
format.js{} # this will allow rails to look for create.js.erb in your views/controller directory where you can use your js to render your partial or append newly created todo
else
format.html{render "new"}
end
end
end
create.js.erb 템플릿에서 js를 사용하여 부분적으로 렌더링하거나 새로 만든 항목을 추가하십시오.
$("#some-id").html("<%=j render "show" %>");
또는
전체 부분을 다시 렌더링하는 대신 js로 테이블에 새로 만든 할 일을 추가하기 만하면됩니다.
$("#table-id").append("<%=j render partial: "todo", locals: {f: @todo} %>");
여기에 _todo.html.erb에
<tr>
<td><%=f.name %></td>
<td><%=f.description%></td>
<td><%=f.is_public %></td>
</tr>
자세한 내용은 체크 아웃 Working with javascript in rails
최신 정보
부분을 보면
<table id="list" border="1" >
<% @todo_lists.each do |f|%> // your @todo_lists is a collection of todos
<tr>
<td><%=f.name %></td>
<td><%=f.description%></td>
<td><%= link_to new_task_path %></td> // you don't need to specify method here link_to by default uses a get request
<td> <%= link_to 'Destroy', f, confirm: 'Are you sure?', method: :delete%></td>
</tr>
<% end %>
</table>
컨트롤러 방법을 다음과 같이 변경하십시오.
def create
@todo_list = ToDoList.new(params[:to_do_list])
@todo_list.member_id = current_member.id
respond_to do |format|
if @todo_list.save
@todo_lists = ToDoList.all // get a collection of todos to render in your partial
format.js{}
format.html { redirect_to @todo_list, notice: 'Lista ' +@todo_list.name+ ' foi criada com sucess!' }
format.json { render json: @todo_list, status: :created, location: @todo_list }
else
format.html { render action: "new" }
format.json { render json: @todo_list.errors, status: :unprocessable_entity }
end
end
end
그리고 create.js.erb 파일에서
$("#show_list").html('<%=j render partial: "to_do_lists/show", locals: { todo_lists: @todo_lists } %>'); //You don't have an element with id = todos. You have it with show_lists so change that
또한 양식을 닫은 후 부분을 렌더링하십시오. 양식 안에 할 일을 표시하는 것은 의미가 없습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다