I’m trying to curl POST my Rails application in order to create a new Entry object. The problem is my entries_controller Create action looks like this:
def create
@user = current_user
@entry = @user.entries.build(params[:entry])
respond_to do |format|
if @entry.save
format.html { redirect_to landing_page_url, notice: 'Entry was successfully created.' }
format.json { render json: @entry, status: :created, location: @entry }
else
format.html { render action: "new" }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
end
Calling @user.entries.build just returns an exception because current_user doesn’t exist. The thing is the Create action works well when I use the browser to create an Entry (as I login and create the current_user variable) but I do not know if it’s possible to curl POST and create an Entry without changing the controller logic. And if it’s not possible, could someone help reach the right direction towards building the controller logic (compatible with curl POST)?
I’m sorry if this is a dumb question, I’m fairly new to all this.
PS: I’m using Rails 3.2.3, if that’s of any help.
Not sure how you create current_user (what it’s based off), but this must exist for your method to work.
If you can pass a param to your action that specifies the user and set current_user as
current_user ||= params[:user]… beware of the security implications of this.You really should have a before_filter on your action to set current_user (via login, I’m presuming).