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


I am trying to use the following URL to make any bookmark an object:

< Pre> http: // localhost: 3000 / posts? Title = Welcome and body = to

But it takes me into index action How do I get to point to the action of making it? I know that both indexes and sequences sequentially share the same URL using GET and POST, but I'm not sure how to use the URL. Thanks for reading.

I'm not sure, but ... I think the rail uses 3 different If you create the link_to 'whatever' approach compared to @FRKT's answer, then new_somethings_path ,: method = & gt; : Post This just adds some HTML5 fields:

  & lt; A href = "/ somethings" data-method = "post" rel = "nofollow" & gt; Whatever & lt; / A & gt;  

And it uses JS to create a form and submit it. Here is some JS code from rails.js :

  function handlemap (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", verb: url, style: "display: none;"}); Element.parentNode.insert (form); If (method! == 'post') {var field = new element ('input', {type: 'hidden', name: '_method', value: method}); Form.insert (area); } 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 (area); } Form.submit (); }  

So I think the _method in the parameter will not work.

So what can you do?

Check only a few parameters in the index operation:

  DRAF index if params [: title] @post = post.NEW: title = & Gt; Paramus [: title] ,: body = & gt; Params [: body] if @ post.save ... #Save successful #include verification error end ... #imple general index code  

put all of you In other ways, creating and saving object content, so your index controller may look cleaner.

You can also create a custom path for this purpose and custom action:

 # Code = 'bookmarklet' = & gt; The name of the post # bookmarket has been placed above the code code  index  with the code controller  bookmarklet  

.


Comments