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:in `block 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

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Dharmdip Rathod at Feb 20, 2012 at 8:18 am
    Hi soichi,
    Please set proper routes may solve your problem.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Feb 20, 2012 at 9:41 am

    On 20 February 2012 01:20, Soichi Ishida wrote:
    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
    Have a look at the Rails Guide on Routing to find how to get it to go
    to the correct action.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Soichi Ishida at Feb 20, 2012 at 10:44 am
    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 20, '12 at 1:20a
activeFeb 20, '12 at 10:44a
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Soichi Ishida: 3 posts Colin Law: 1 post

People

Translate

site design / logo © 2021 Grokbase