ArgumentError in create action

15 views
Skip to first unread message

craigw...@gmail.com

unread,
Oct 9, 2014, 7:15:37 AM10/9/14
to praxis-...@googlegroups.com
Hello,

I'm writing an application that manages articles and I'm having a little trouble with the create action. I'm using Sequel to store data. Here is the relevant section of my resource definition:

action :create do
routing { post '' }
response :created

payload do
attribute :title
attribute :content
attribute :published_at
end
end

And here is the relevant section of my controller:

def create
article = Article.create do |article|
article.title = request.payload.title
article.content = request.payload.content
article.published_at = request.payload.published_at
end

response.headers['Content-Type'] = 'application/json'
response.body = article.values
response
end

When I post an article, it gets created in database but the response fails. Here is the error, if you like, I can post the backtrace too:

"message": "wrong number of arguments (1 for 0)"

Any help is much appreciated.

Kind Regards,
Craig Walsh.

Josep Blanquer

unread,
Oct 9, 2014, 11:55:33 AM10/9/14
to craigw...@gmail.com, praxis-...@googlegroups.com
Hi Craig,

 Yes this is a slightly funny thing that we need to change because it is confusing and it is really unnecessary to be this way.

The gist of the problem is that we're always expecting the controller methods to take a hash of arguments corresponding to your defined parameters in the resource definition. That hash would be empty when there are no defined parameters like in your case, but we'll still pass it in the method invokation, therefore causing your "wrong number of arguments (1 for 0)"

You can solve that issue by redefining your method signature to take a hash (or a splatted hash) and then ignore it since it'd be empty:

def create(**args)
...
end

I've opened an issue (https://github.com/rightscale/praxis/issues/58) to fix it. Once that's done you can go back to using a parameterless method like you're doing right now.

Thank you for the feedback!

Josep M.



--
You received this message because you are subscribed to the Google Groups "praxis-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to praxis-suppor...@googlegroups.com.
To post to this group, send email to praxis-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/praxis-support/6645776b-adc6-4790-a060-896a4a22905b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

craigw...@gmail.com

unread,
Oct 9, 2014, 2:48:09 PM10/9/14
to praxis-...@googlegroups.com, craigw...@gmail.com
Thanks Josep that has worked a treat. I'm not sure if this is a related issue but now I get the following error message:

"name": "Praxis::Exceptions::InvalidResponse",
"message": "Response :ok is not allowed for :create"

This is strange because I am returning the :created response in the resource definitions create action. If I change the response to :ok, the error message goes away and everything works as expected. Does this seem backwards to you?

P.S. I'm really enjoying using Praxis, keep up the hard work!

Kind Regards,
Craig Walsh.

Josep Blanquer

unread,
Oct 9, 2014, 3:50:26 PM10/9/14
to craigw...@gmail.com, praxis-...@googlegroups.com
On Thu, Oct 9, 2014 at 11:48 AM, <craigw...@gmail.com> wrote:
Thanks Josep that has worked a treat. I'm not sure if this is a related issue but now I get the following error message:

"name": "Praxis::Exceptions::InvalidResponse",
"message": "Response :ok is not allowed for :create"

This is strange because I am returning the :created response in the resource definitions create action. If I change the response to :ok, the error message goes away and everything works as expected. Does this seem backwards to you?


Craig,

What this message says is:
-- A response class of the type ":ok" is not one of the responses allowed in the "create" resource definition.
(which probably means that we could tweak the message to be more clear)

So, what I can bet it is happening is that you are returning the default response in the controller code for the "create" action, which is "Responses::Ok" (and of name :ok).
What you need to do is to return the right response class from your controller (as you've said you'd do in your response definition)

For example, something like:

def create 
  ...code...
  Praxis::Responses::Created.new(headers: {'Location'=> '/somelocation/id'} )
end

or simply change the default response at the beggining of the action with (self.response=Praxis::Responses::Created.new) and then set the headers in other parts of the controller.

Cheers,

Josep M.

Reply all
Reply to author
Forward
0 new messages