Hi, I''m very new to Ruby and to Rails and I''m trying to get to grips with it. One of things I''m trying to do is to build a very simple web application where, essentially, people can look up films and post reviews about the films. I''ve created a database in MySQL with a Reviews table which includes table columns: id, review_text, person_id and film_id. id is the primary key, Person_id and film_id are foreign keys that I''m trying to pass into that table. Can anyone please, please tell me what syntax I need to use in the VIEW in order to pass all of the data to the controller and save it in the database? Some of my code is below - I haven''t yet built a person login so I''ve simply assigned a person_id. I''ve seem to have tried every random bit of syntax I can think of and review_text gets submitted but I can''t pass person_id or film_id so it won''t save in the database. EXTRACTED CODE FROM VIEW <%= @film.id %> <%= @person.id = 22 %> <%= start_form_tag :action => "create_review", :id => @film %> <p> </p> <p><b>Review it:</b></p> <p><%= text_area "review", "review_text" %></p> <p> </p> <%# "film_id" = @film.id %> <%# "person_id" = @person.id %> <%= submit_tag "create" %> </form> EXTRACTED CODE FROM CONTROLLER def add_review #@review = Review.new (params[:review]) @person = Person.new @film = Film.find(params[:id]) end def create_review @review = Review.new(params[:review]) if @review.save flash[:notice] = ''review was successfully created.'' redirect_to :action => ''view_review'' else render :action => ''new_review'' end end -- Posted via http://www.ruby-forum.com/.
Hi Daniel - For starters I don''t see in your form how your film_id and person_id are going to get passed into the action since they''re not form fields (and they''re commented out). Your scenario is kind of half baked at the moment but to help in getting it working at the minimum you should have those two id fields stored in a <input type="hidden" /> field in rails you do this with: hidden_field(:variable, :attribute, options) Also I''m not sure why you''re start_form_tag has an :id => @film in it. That''s not doing anything but I''m not sure what you were trying to do. I would also suggest that you actually create the model objects that you''re going to be using in the real world so you''re not creating problems due to an unimplemented model. script/generate scaffold will make this very easy to test. Mike On 11/30/05, Daniel Potter <dannypotta-YDxpq3io04c@public.gmane.org> wrote:> Hi, > > I''m very new to Ruby and to Rails and I''m trying to get to grips with > it. One of things I''m trying to do is to build a very simple web > application where, essentially, people can look up films and post > reviews about the films. > > I''ve created a database in MySQL with a Reviews table which includes > table columns: id, review_text, person_id and film_id. id is the > primary key, Person_id and film_id are foreign keys that I''m trying to > pass into that table. > > Can anyone please, please tell me what syntax I need to use in the > VIEW in order to pass all of the data to the controller and save it in > the database? Some of my code is below - I haven''t yet built a person > login so I''ve simply assigned a person_id. I''ve seem to have tried > every random bit of syntax I can think of and review_text gets submitted > but I can''t pass person_id or film_id so it won''t save in the database. > > > EXTRACTED CODE FROM VIEW > > <%= @film.id %> > <%= @person.id = 22 %> > > <%= start_form_tag :action => "create_review", :id => @film %> > <p> </p> > <p><b>Review it:</b></p> > <p><%= text_area "review", "review_text" %></p> > <p> </p> > <%# "film_id" = @film.id %> > <%# "person_id" = @person.id %> > <%= submit_tag "create" %> > </form> > > > EXTRACTED CODE FROM CONTROLLER > > def add_review > #@review = Review.new (params[:review]) > @person = Person.new > @film = Film.find(params[:id]) > end > > def create_review > @review = Review.new(params[:review]) > if @review.save > flash[:notice] = ''review was successfully created.'' > redirect_to :action => ''view_review'' > else render :action => ''new_review'' > end > end > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi, Daniel, IMHO you''re approaching this thing from the wrong side. Why are you working on the review-component while your film-component seems to be not up at all? In your design a film seems to be the central model. You should model a film- and a review-model as it is explained on the community sites or in Agile Web Development with rails. A film then should have a one to many relationship to the review-model. Once you''ve got this up you might create some test-data through the scaffolded admin-view and then you might add reviews to that film. This is exactly the same as DHH does in the introduction video with articles (~films) and comments (~reviews), so just have a look at that video and you should get the idea. regards Jan Daniel Potter wrote:>Hi, > > I''m very new to Ruby and to Rails and I''m trying to get to grips with >it. One of things I''m trying to do is to build a very simple web >application where, essentially, people can look up films and post >reviews about the films. > > I''ve created a database in MySQL with a Reviews table which includes >table columns: id, review_text, person_id and film_id. id is the >primary key, Person_id and film_id are foreign keys that I''m trying to >pass into that table. > > Can anyone please, please tell me what syntax I need to use in the >VIEW in order to pass all of the data to the controller and save it in >the database? Some of my code is below - I haven''t yet built a person >login so I''ve simply assigned a person_id. I''ve seem to have tried >every random bit of syntax I can think of and review_text gets submitted >but I can''t pass person_id or film_id so it won''t save in the database. > > >EXTRACTED CODE FROM VIEW > ><%= @film.id %> ><%= @person.id = 22 %> > > <%= start_form_tag :action => "create_review", :id => @film %> > <p> </p> > <p><b>Review it:</b></p> > <p><%= text_area "review", "review_text" %></p> > <p> </p> > <%# "film_id" = @film.id %> > <%# "person_id" = @person.id %> > <%= submit_tag "create" %> > </form> > > >EXTRACTED CODE FROM CONTROLLER > >def add_review > #@review = Review.new (params[:review]) > @person = Person.new > @film = Film.find(params[:id]) >end > >def create_review > @review = Review.new(params[:review]) > if @review.save > flash[:notice] = ''review was successfully created.'' > redirect_to :action => ''view_review'' > else render :action => ''new_review'' > end > end > > >
On 30.11.2005, at 15.19, Michael Engelhart wrote:> Also I''m not sure why you''re start_form_tag has an :id => @film in it. > That''s not doing anything but I''m not sure what you were trying to > do.Actually, it does. It passes the id of the @film to the receiving action so that it can be used as params[:id]. This all assuming that the @film is a valid and saved Film object. So when using this you won''t have to use a hidden field for the film id. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael beat me to the answer, but I''d just like to throw in an extra two pieces of advice: 1) Email subject lines should describe the subject of your email. A subject like "PLEASE HELP!" might not get read by the right person where a subject like "How do I pass data from views to controllers?" would. 2) Use <%= end_form_tag %> to close your form. Yours, Craig -- Craig Webster | t: +44 (0)131 516 8595 | e: craig-07VhxHapISisTnJN9+BGXg@public.gmane.org Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net
Couldn''t the form tag be written <%= form_remote_tag :url => { :action => :someControllerMethod, :filmid=>@film.id, :personid=>@person.id }%> That should allow you to refer to params[:filmid] and params[:personid] in the controller. John Daniel Potter wrote:>Hi, > > I''m very new to Ruby and to Rails and I''m trying to get to grips with >it. One of things I''m trying to do is to build a very simple web >application where, essentially, people can look up films and post >reviews about the films. > > I''ve created a database in MySQL with a Reviews table which includes >table columns: id, review_text, person_id and film_id. id is the >primary key, Person_id and film_id are foreign keys that I''m trying to >pass into that table. > > Can anyone please, please tell me what syntax I need to use in the >VIEW in order to pass all of the data to the controller and save it in >the database? Some of my code is below - I haven''t yet built a person >login so I''ve simply assigned a person_id. I''ve seem to have tried >every random bit of syntax I can think of and review_text gets submitted >but I can''t pass person_id or film_id so it won''t save in the database. > > >EXTRACTED CODE FROM VIEW > ><%= @film.id %> ><%= @person.id = 22 %> > > <%= start_form_tag :action => "create_review", :id => @film %> > <p> </p> > <p><b>Review it:</b></p> > <p><%= text_area "review", "review_text" %></p> > <p> </p> > <%# "film_id" = @film.id %> > <%# "person_id" = @person.id %> > <%= submit_tag "create" %> > </form> > > >EXTRACTED CODE FROM CONTROLLER > >def add_review > #@review = Review.new (params[:review]) > @person = Person.new > @film = Film.find(params[:id]) >end > >def create_review > @review = Review.new(params[:review]) > if @review.save > flash[:notice] = ''review was successfully created.'' > redirect_to :action => ''view_review'' > else render :action => ''new_review'' > end > end > > >
Thanks very much all. I think I''ll watch the video first and then try each of the various suggestions in turn. As I said, I''m very new to rails and still have very little clue what I''m doing but I guess I''ll learn by trying. Thanks again, Danny -- Posted via http://www.ruby-forum.com/.