What's calling update action?

Rails 3.1.3

Hi. I have defined a new action, create_or_update, namely, it creates
if doesn’t exist, update if exists.

def create_or_update
@script =
Script.find_or_create_by_startp_and_video_id(params[:startp],
params[:video_id])
reposnd_to do |format|
@script.update_attributes({
:id => self.id,
:startp => self.startp,
:text => self.text
})
end
end

And in the view,

<%= render :partial => "create_or_update_script", :locals => {

:script => Script.find_or_create_by_video_id(:video_id => @video.id)} %>

(hoping that it) renders, _create_or_update_script.html.erb

<%= form_for script,
:url=>{:controller=>‘scripts’, :action=>‘create_or_update’},
:remote => true do |f| %>
<%= f.hidden_field :video_id %>
<%= f.text_field :startp, :readonly => true %>
<%= f.text_field :text %>
<%= f.submit “create_or_update” %>
<% end %>

I specified both the controller and the action. If I try to save a new
entry, it gives an error,

Started PUT “/scripts/create_or_update” for 127.0.0.1 at 2012-02-19
19:53:33 +0900
Processing by ScriptsController#update as JS
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“VoFDQN3sbqdUmiYEd8E54IlTI3yJPuNpMf9sc2Gmtho=”,
“script”=>{“video_id”=>“18”, “startp”=>“41”, “text”=>“how this is
happening”}, “commit”=>“create_or_update”, “id”=>“create_or_update”}
Script Load (0.1ms) SELECT “scripts”.* FROM “scripts” WHERE
“scripts”.“startp” IS NULL LIMIT 1
Completed 500 Internal Server Error in 122ms
NoMethodError (undefined method startp' for #<ScriptsController:0x00000129eb5120>): app/controllers/scripts_controller.rb:79:inblock in update’
app/controllers/scripts_controller.rb:76:in `update’

It is calling “app/controllers/scripts_controller.rb:79:in `block in
update’”, which is

def update
@script = Script.find_by_startp(params[:startp])
respond_to do |format|
if @script.update_attributes(params[:script])
format.json { head :ok }
else
format.html { render action: “edit” }
format.json { render json: @script.errors, status:
:unprocessable_entity }
end
end
end

Also, in the error, ““id”=>“create_or_update”” which is supposed to be
an integer for the id of ‘script’, if I understand Rails correctly.

My question is:

  1. Why is it calling the update action? (I am guessing it’s due to
    routes.rb)
  2. How can I pass the proper id for ‘Script’ ?

Thanks in advance.

soichi

Hi soichi,
Please set proper routes may solve your problem.

On 20 February 2012 01:20, Soichi I. [email protected] wrote:

reposnd_to do |format|

<%= f.text_field :text %>
<%= f.submit “create_or_update” %>
<% end %>

I specified both the controller and the action. If I try to save a new
entry, it gives an error,

Started PUT “/scripts/create_or_update” for 127.0.0.1 at 2012-02-19
19:53:33 +0900
Processing by ScriptsController#update as JS

Have a look at the Rails Guide on Routing to find how to get it to go
to the correct action.

Colin

Thanks all.

I put in routes.rb

resources :scripts do
member do
put ‘create_or_update’
end
end

then, it recognizes the action I defined!

soichi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs