简体   繁体   English

如何使用URL链接到Ruby on Rails中的create操作?

[英]How do I use a URL to link to the create action in Ruby on Rails?

I'm trying to use the following URL to create an object from a bookmarklet: 我正在尝试使用以下URL从书签创建一个对象:

http://localhost:3000/posts?title=Welcome&body=To

but it takes me to the index action. 但这将我带到了索引操作。 How do I get it to point to the create action? 如何获得指向创建动作的信息? I know that both index and create share the same URL, using GET and POST respectively, but I'm not sure how to specify which to use from a URL. 我知道分别使用GET和POST的index和create共享相同的URL,但是我不确定如何指定从URL使用哪个URL。 Thanks for reading. 谢谢阅读。

I'm not sure, but... I think that Rails 3 uses different approach than @FRKT's answer. 我不确定,但是...我认为Rails 3使用的方法与@FRKT的答案不同。 If you create link_to 'whatever', new_somethings_path, :method => :post it just add some HTML 5 fields: 如果创建link_to 'whatever', new_somethings_path, :method => :post则只需添加一些HTML 5字段即可:

<a href="/somethings" data-method="post" rel="nofollow">whatever</a> 

and it uses js to create a form and submit it. 它使用js创建表单并提交。 Here is some js code from rails.js : 这是来自rails.js一些js代码:

function handleMethod(element) {
  var method = element.readAttribute('data-method'),
    url = element.readAttribute('href'),
    csrf_param = $$('meta[name=csrf-param]')[0],
    csrf_token = $$('meta[name=csrf-token]')[0];

  var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
  element.parentNode.insert(form);

  if (method !== 'post') {
    var field = new Element('input', { type: 'hidden', name: '_method', value: method });
    form.insert(field);
  }

  if (csrf_param) {
    var param = csrf_param.readAttribute('content'),
      token = csrf_token.readAttribute('content'),
      field = new Element('input', { type: 'hidden', name: param, value: token });
    form.insert(field);
  }

  form.submit();
}

So I think that _method in params would not work. 因此,我认为params中的_method无效。

So what you can do? 那你能做什么?

In index action just add some params checking: index操作中,只需添加一些参数检查:

def index
  if params[:title] 
    @post = Post.new :title => params[:title], :body => params[:body]
    if @post.save
      ...  # successful save
    else
      ...  # validation error
    end
  end

  ... # put normal index code here
end

You can put all createing and saveing object stuff in other method, so your index controller may look cleaner. 您可以将所有创建和保存对象的内容放入其他方法中,以便索引控制器看起来更干净。

You can also create a custom route for this purpose and custom action: 您也可以为此目的和自定义操作创建自定义路线:

# routes 
match 'bookmarklet' => 'posts#bookmarklet'

Then put above code from index action to bookmarklet action in posts controller. 然后将以上代码从index动作放置到posts控制器中的bookmarklet动作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:我可以使用“link_to”来调用创建操作吗? - Ruby on Rails: Can I do a “link_to” to call a create action? 如何在Rails Controller / Action中使用Ruby改进? - How do I use Ruby refinements inside Rails Controller/Action? Ruby on Rails-如何创建传递参数的网址? - Ruby on Rails - how do I create a url to pass a parameter? Ruby on Rails-如何在网址中使用破折号而不是下划线? - Ruby on Rails - How do I use dashes instead of underscores in the url? 如何在Rails中创建传递自定义操作ID的链接?0 &lt;%= link_to“ PDF”,:action =&gt;“ showpdf”,:id =&gt;“ #{@letter.id}.pdf”% &gt; - How do I create a link that passes an ID for a custom action in Rails?0 <%= link_to “PDF”, :action => “showpdf”, :id => “#{@letter.id}.pdf” %> Ruby on Rails如何创建链接 - Ruby on rails how create link 如何创建Rails控制器动作? - How do I create a rails controller action? 如何使用页面ID /名称加载正确的控制器操作? (Ruby on Rails 4) - How do I use a page id/name to load the correct controller action? (Ruby on Rails 4) 如何在Ruby on Rails中创建指向字段X包含Y的记录列表的链接? - How do I create a link to a list of records where field X contains Y in Ruby on Rails? 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM