初学rails项目总结(一)

本文介绍了初学者在学习Rails时遇到的URL参数处理和JavaScript交互问题。通过实例展示了如何获取URL中的参数,利用js传递参数以及在Rails中处理select标签的选择值。还讲解了在动态环境下如何截取URL前缀以适应不同的部署场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function commit()
    {
        var length = window.document.location.href.indexOf(location.pathname);
        var http = window.document.location.href.substring(0,length);
        var v =  $('#system_id').find(':selected').val();
        window.location.href = ""+http+"/boards?system_id="+v+""; //这个是跳转到指定url
    }

http://localhost:3000/boards?system_id=000q000926XUkvMQawrCam 为例
location.pathname:/boards
location.href:http://localhost:3000/boards?system_id=000q000926XUkvMQawrCam 就是完整的url
location.href.indexOf(location.pathname):21 从开始到pathname之前有多少个字母
var http = window.document.location.href.substring(0,length); 从0到pathname之前的截取出来,因为当你在本地跑项目的时候,前缀是localhost,但实际开发项目中,可能时www开始的域名,所有跳转到指定的url并传参数,可以使用这种方法,既可以在本地跑,发布到服务器上也可以跑。

js调用 提交有一个onclick事件
js可以通过url进行值传递,具体写法是 ?加变量名=”+var变量+”
取得下拉框的值:$(‘#下拉框的name’).find(‘:selected’).val();
向页面发送请求,但是域名不是固定的不是localhost 需要把前面固定的截取出来

<div style="display:inline-block;">
      <label for="order_system_type" style="display: inline-block">Select System:</label>
      <%= select_tag_alias 'system_id', @system_ids %>
      <input type="button" value="submit" onclick = "commit()">
</div>

select_tag_alias标签:第一个是下拉框的name,第二个是变量一个数组 数组第一个值是key就是显示的值 第二个值是value选中以后需要传的值 取出来的时候需要collect

system_id = params[:system_id];
@system_ids = []
@system_ids = Irm::ExternalSystemsTl.
       where("#{Irm::ExternalSystemsTl.table_name}.external_system_id in (select external_system_id from #{Irm::ExternalSystemPerson.table_name} where person_id = '#{ Irm::Person.current.id }')").
       where("#{Irm::ExternalSystemsTl.table_name}.language = 'en'").
       collect { |i| [i[:system_name], i[:external_system_id]] }

没有collect的话 取出来的是一个Irm::ExternalSystemsTl对象,如果collect以后,是一个数组,数组key和value为第一个和第二个的值

<td class="label-col"><label><%= t(:knowledge_base) %></label></td>
      <td class="data-col">
        <!--<input type="checkbox" name="program_params[no_flag]" id="no_flag" onclick="set_value(this)"/>-->
        <%= check_box_tag 'program_params[no_knowledge]' %>

显示一个选择框,当click时会触发一个set_value方法。

$(function(){
        if('<%=program_params[:no_knowledge]%>' == '1'){
            $("input[name='program_params[no_knowledge]']").prop("checked",true)
        }
    })

jquery:如果program_params[:no_knowledge]为1,则会被选中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值