我有一个index.html.erb页面时间表,它有各种js和jquery表和函数。我想从页面中取出字段并将它们提交给我在ruby / rails 3中的数据库模型。
我想从页面中获取的字段包括由javascript函数生成的文本字段,由Span Div生成的日期以及必须从纯文本(或其他方式)中找到的ID。来自这些字段的值必须输入到称为Efforts的轨道模型上的ruby中。我知道如何通过函数在控制器中正常执行此操作:
effort = Efforts.create!(
:project_task_id => (get from the plain text ),
:User_id => current_user.id,
:week_commencing => (get from a span div),
:hours => (get from text field) )
问题是获得我想要的值,以上面的格式由ruby读取。
代码: index.html.erb
<%= stylesheet_link_tag 'demo_table2'%>
<%= javascript_include_tag 'jquery.dataTables2.min'%>
<fieldset>
<h1 class="head">Timesheet</h1>
<div class="left">
<div class="week-picker"></div>
</div>
<div class="left">
<b>Project Selector</b>
<select id="project_selector" onchange="$.ajax('/projects/' + $('#project_selector').val() + '/project_tasks.js');">
<% @projects.each do |project| %>
<option value="<%= project.id %>"><%= project.project_number %> <%= project.project_name %></option>
<% end %>
</select>
<table border="2" width="" id='tasks' class='datatable'>
<thead>
<tr>
<th>Task</th>
<th></th>
</tr>
</thead>
<tbody id='tasks_tb'>
<!--
<% @projects.each do |project| %>
<tr>
<td><%= project.project_number %></td>
<td><%= project.project_name %></td>
<td><%= link_to_function image_tag("icons/add.png"), "CreateNewRow('#{project.project_number}','#{project.project_name}')", :remote => true %></td>
</tr>
<%- end -%>
-->
</tbody>
</table>
</div>
<div class="right">
<b>Recently Viewed</b>
<table>
<tr>
<th>Project No.</th>
<th>Project names</th>
<th></th>
</tr>
<tr>
<td>123</td>
<td>Test</td>
<td><%= link_to image_tag("icons/add.png") %></td>
</tr>
</table>
</div>
</fieldset>
<fieldset>
<b><center>Hours for Week commencing: <span id="startDate"><%= Date.today.beginning_of_week.strftime('%d/%m/%Y') %></span></center></b>
</fieldset>
<!-- Task list table -->
<!-- Hours list table -->
<fieldset>
<div class="left">
<table>
<tr>
<td>Leave</td>
<td><input class="dayinput" type="text" name="Leave" placeholder="0"></td>
</t>
<tr>
<td>TOIL</td>
<td><input class="dayinput" type="text" name="TOIL" placeholder="0"></td>
</tr>
<tr>
<td>Sick</td>
<td><input class="dayinput" type="text" name="Sick" placeholder="0"></td>
</tr>
<tr>
<td>Total</td>
<td><input id="total" class="total_low" type="text" value="0" disabled="">
</tr>
</table>
</div>
<div class="right">
<b>Tasks this week</b>
<ul id="task_list">
<form name="frmMain" method="post">
<table width="470" border="1" id="tbExp">
<tr>
<td><div align="left">Task Name</div></td>
<td><div align="left">Hours </div></td>
<td><div align="center"></div></td>
</tr>
</table>
<input type="hidden" name="hdnMaxLine" value="0">
</form>
</ul>
<button>Submit</button>
</div>
</fieldset>
<%= javascript_include_tag 'timesheet'%>
js for addng a field
if( $('#effort_<%= @project_task.id %>').length == 0 )
$('#task_list').append('<tr><td><%= @project_task.project.project_number %> <%= @project_task.project.project_name %> - <%= @project_task.task_name %>' +
'<td><%= text_field :effort, :hours, :name => 'effort_' + @project_task.id.to_s, :id => 'effort_' + @project_task.id.to_s %>' +
'<td><%= link_to image_tag('icons/delete.png') %></tr>' );
用于显示任务的js
$('#tasks_tb').html('');
<% @project.project_tasks.each do |task| %>
$('#tasks_tb').append('<tr><td><%= task.task_name %><td><%= link_to image_tag("icons/add.png"), addtimesheettask_path( task.id ), :remote => true %>');
<% end %>
我想按照我想要的方式吠叫错误的树吗?我对rails也很新。
答案 0 :(得分:0)
你想做的一切都很有可能。您所追求的是rails forms(http://guides.rubyonrails.org/form_helpers.html)。如果您按照指南通过rails帮助程序创建表单,则在提交表单时,您已通过params
哈希提交表单的控制器中可以使用各种表单字段中的所有数据。 / p>
您需要做的就是从params哈希中提取各种数据并构建新的模型对象。您需要遵循的步骤是:
new
和create
),在这里阅读控制器基础知识(http://guides.rubyonrails.org/action_controller_overview.html)