开发者

getting values from various formats and submitting to a Rails model

I have an index.html.erb page timesheet that has various js and jquery tables and functions. I want to take fields from the page and submit them to the database model i have in ruby/rails 3.

The fields i want from the page include a textfield that gets generated by javascript function, A Date that gets generated by a Span Div and an id that has to be found from plain t开发者_如何学Pythonext (or some other way). The values from these fields have to be inputted into a ruby on rails model called Efforts. I know how to do this normally in the controller by a function :

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) )

The problem is getting the values i want that are able to be read by ruby in the format above.

code: 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 for showing the tasks

$('#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 %>

Am i barking up the wrong tree in the way i want to do this? im quite new to rails aswell.


Everything you want to do is very possible. What you're after is rails forms (http://guides.rubyonrails.org/form_helpers.html). If you create a form via the rails helpers as per the guide, when you submit the form all the data from the various form fields will be available in the controller you've submitted the form to via the params hash.

All you will have to do is pull the various pieces of data from the params hash and construct your new model object. The steps you need to follow are:

  • read the guide about form helpers to make sure you understand how that hangs together
  • create a controller such as EffortsController, this will need at least two of the 7 restful actions (new and create), read about controller basics here (http://guides.rubyonrails.org/action_controller_overview.html)
  • create some routes to map urls to your controller actions, read about routing here (http://guides.rubyonrails.org/routing.html)
  • create a view using what you learned about forms
  • when your page loads execute your javascript to generate whatever dynamic stuff you need, the goal for you is to make sure all pieces of data you want available to you end up in a form control, some of these might be visible some might be hidden (if you're pulling some information from another part of the page via javascript and don't want the user to see it)
  • once you submit the form you will access to all the data you need
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜