权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - rails 5, remote: true form empty data - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - rails 5, remote: true form empty data - Stack Overflow

programmeradmin8浏览0评论

I am trying to migrate Rails 4.2 application to Rails 5.1. In rails 4.2 we heavily used JQuery. At the moment I am struggling to make the forms, with remote: true attribute work properly. As an example this is a simple form where user select the country

= simple_form_for(:user_data,
  url: user_path(@user),
  remote: true,
  method: :patch,
  data: {'user-update' => true},
  dataType: 'json',
  html: wrapper: :horizontal_form ) do |f|
  .panel.panel-default
    .panel-body
      .col-md-8
        = f.input :country, label: 'Country',
      = f.button :submit  

The controller

def update
  @user.update!(user_params)
  render json: @user
end

I have tried to add the respond_to with js response format, but in that case it tries to convert the @user to executable javascript. As of the event handler looks like this

  $('form[data-user-update]')
  .on('ajax:success', function(e, data, status, xhr) {
     // The data variable is empty
  })
  .on('ajax:error', function(e, error, status, xhr) {
    $('.simple_form').renderFormErrors('user_data', error.responseJSON);
  });

EDIT

Response Header

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
ETag: W/"f53889092c58dc37054386c9504ad1ff"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 712b59ff-8011-4b10-a905-83e559b47452
X-Runtime: 0.101165
Transfer-Encoding: chunked

Request Header

Accept:text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,et;q=0.6
Connection:keep-alive
Content-Length:71
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/users/470d573b-b0f1-4822-b036-7d37be6672d6
X-Requested-With:XMLHttpRequest

Response

{ 
  firstName:'John',
  lastName: 'Smith',
  country: 'USA'
}

I am trying to migrate Rails 4.2 application to Rails 5.1. In rails 4.2 we heavily used JQuery. At the moment I am struggling to make the forms, with remote: true attribute work properly. As an example this is a simple form where user select the country

= simple_form_for(:user_data,
  url: user_path(@user),
  remote: true,
  method: :patch,
  data: {'user-update' => true},
  dataType: 'json',
  html: wrapper: :horizontal_form ) do |f|
  .panel.panel-default
    .panel-body
      .col-md-8
        = f.input :country, label: 'Country',
      = f.button :submit  

The controller

def update
  @user.update!(user_params)
  render json: @user
end

I have tried to add the respond_to with js response format, but in that case it tries to convert the @user to executable javascript. As of the event handler looks like this

  $('form[data-user-update]')
  .on('ajax:success', function(e, data, status, xhr) {
     // The data variable is empty
  })
  .on('ajax:error', function(e, error, status, xhr) {
    $('.simple_form').renderFormErrors('user_data', error.responseJSON);
  });

EDIT

Response Header

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
ETag: W/"f53889092c58dc37054386c9504ad1ff"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 712b59ff-8011-4b10-a905-83e559b47452
X-Runtime: 0.101165
Transfer-Encoding: chunked

Request Header

Accept:text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,et;q=0.6
Connection:keep-alive
Content-Length:71
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/users/470d573b-b0f1-4822-b036-7d37be6672d6
X-Requested-With:XMLHttpRequest

Response

{ 
  firstName:'John',
  lastName: 'Smith',
  country: 'USA'
}
Share Improve this question edited Aug 10, 2017 at 14:30 mr. Holiday asked Aug 10, 2017 at 14:02 mr. Holidaymr. Holiday 1,8002 gold badges21 silver badges39 bronze badges 8
  • Can you inspect via the developer tools the server's response? (in Chrome, tab "network", do the form remote submit, find the request and analyze the HTTP req/res) – MrYoshiji Commented Aug 10, 2017 at 14:22
  • @MrYoshiji please see the update – mr. Holiday Commented Aug 10, 2017 at 14:40
  • if you put a debugger in the ajax:success callback, are you able to see any value for e, data, etc? – MrYoshiji Commented Aug 10, 2017 at 14:54
  • Values are undefined – mr. Holiday Commented Aug 10, 2017 at 14:55
  • The Value of data, status and xhe, are undefined. The e holds the JQuery.event object – mr. Holiday Commented Aug 10, 2017 at 15:04
 |  Show 3 more ments

1 Answer 1

Reset to default 11

Just figured it out while running into the very same problem.

As per http://edgeguides.rubyonrails/working_with_javascript_in_rails.html#dealing-with-ajax-events

I assume you're using rails-ujs instead of older jquery-ujs. In jquery-ujs the code would work and data, status, xhr would be returned; rails-ujs however only returns one attribute - event and additional things are accessed through array on event.details

As per example in the guide

document.body.addEventListener('ajax:success', function(event) {
  var detail = event.detail;
  var data = detail[0], status = detail[1],  xhr = detail[2];
})

You can access response by calling event.detail[0]

If you've got quite a bit of code relying on this functionality, probably a better bet is to change rails-ujs back to jquery-ujs

发布评论

评论列表(0)

  1. 暂无评论