ruby - Rails undefined method -


i've been trying first rails code work while , still can't seem work. latest problem keep getting following error

'nomethoderror in messagecontroller#create".

i see trying pass nil variables shown in next line

"undefined method `message' #<message mess: nil, user: nil>" 

however parameters passed correctly can see in bottom of error screen. code below. appreciated.

controller

class messagecontroller < applicationcontroller   def new     @message = message.new   end    def create     @message = message.new(user_params)     @message.mess=params[:mess]     @message.user=params[:user]     if @message.save         redirect_to "http://itworks.com"     else         redirect_to "http://myspace.com"     end   end  private   def user_params     params.require(:message).permit(:mess, :user)   end end 

view

<%= form_for @message, url: {action: "create"}, html: {class: "nifty_form"} |f| %>      <%= f.text_field :mess %><br />      <%= f.text_field :user %><br />     <%= f.submit "create" %> <% end %> 

strong parameters

firstly, need read on strong parameters

with rails 4, prevent mass assignment, need create new activerecord objects using private method assign parameter values need:

#app/controllers/messages_controller.rb class messagescontroller < applicationcontroller    def create       @message = message.new(message_params)       @message.save    end     private     def message_params #-> can call method :)        params.require(:message).permit(:mess, :user)    end end 

this set params activerecord object params hash:

params {     "message" => {          "mess" => "value",         "user" => "value"     } } 

this should passed form stands; however, if started use form_tag, you'll end without message param, preventing strong params method working correctly.

--

form

your form can simplified:

<%= form_for @message, html: {class: "nifty_form"} |f| %> 

if populate form_for activerecord object, rails should extract path automatically you; not matters error, form sending request create action anyway

--

method

the no method error derived calling method on object either doesn't exist, or not support method (unsurprisingly).

in respect debugging issue, need may calling .message on object. typically in view or (if not in controller)

with reference .message being in strong params method have, recommend trying this:

 def create     @message = message.new(user_params)     if @message.save         redirect_to "http://itworks.com"     else         redirect_to "http://myspace.com"  end 

this make action conventional, should give best chance of working.

--

association

the other problem may have activerecord association in message model. activerecord associations defined in model give objects ability append associative data

the error have could come this:

#app/models/message.rb class message < activerecord::base     belongs_to :message #-> won't work end 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

django - CSRF verification failed. Request aborted. CSRF cookie not set -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -